Rockson

Rockson

Hi I’m an absolute beginner trying to understand how to achieve the same thing I do with object oriented frameworks.
I can’t understand which is the recommended way to load some data at the start of the request that I can use later in my code without re-elaborating it every time. For example the authenticated user. I load it at the begin of the request, place it in a global store(aka context) and I can access it wherever I want since it is global.

Do I have to pass it using parameters? I don’t like the thing of polluting functions with it.
What does phoenix|elixir recommend?

Showing Posts 1 to 10

gregvaughn

gregvaughn

I think what you want to understand is the conn struct provided by Plug, which Phoenix builds atop. Plugs and plug pipelines can be used in your router to do things like authenticate a user and store that user in the conn struct so that once you resolve a particular controller/action you can access it.

However, you are expected to pass that sort of thing into functions lower-level in your business logic (Phoenix calls them “contexts”) where appropriate.

Rockson

Rockson OP

Thanks for the answer. Yeah I was looking for something like that.
But what if in my Phoenix contexts I use the authenticated user a lot? There would be a lot of functions that includes it. Isn’t it common to use some memoization strategy or a way to store it globally and access from my functions?

Sorry if I insist but i find it annoying at first

gregvaughn

gregvaughn

I would offer to you the argument that is is not “pollution” for a function to accept a user parameter if it depends upon that user to perform its … function properly. That is called functional programming.

There are ways around it, but no, they are not common. What’s common is to have pure functions that take in all the params they need to achieve their function. Those are really easy to write tests for.

kokolegorille

kokolegorille

Remember there are no objects, and FP does not share the same design patterns.

FP is about having pure functions. Everything is about (pure) composable functions…

Rockson

Rockson OP

I probably misphrased it. I meant: I’m doing this thing in oop, is it a thing in elixir or should I do something else?
I’m good with FP principles but maybe I’ve been bad educated by frameworks like React. One way for React to"solve" props passing between sub-components is the Context. In fact React passes the context as a parameter to the component but it allows somehow to get data back from it with it’s utilities. So it still follow the FP rules but has this implicit context passed to components. This way you don’t have the same props passed down to a lot of components. I thought this was a common pattern for FP but looks like it is not

kokolegorille

kokolegorille

The context of a request is the conn structure.

There is no equivalent to React Context… but You have many ways to store shareable data, for example, GenServer, Agent, or ETS table.

BTW I have been using Phoenix with React and I find it an excellent combo. Mostly because it is possible to have 2 data stores, one on the frontend, and one on the server (a GenServer storing shared data for a multiplayer board game)… keeping them sync with channels.

Rockson

Rockson OP

Ok that’s good to know. I guess I need to investigate GenServer, Agent and ETS use cases more. Thanks for your time!

Rockson

Rockson OP

So I was navigating phoenix docs and I saw that whenever you make a query you can use.

Repo.all(...my expresssion...)

Where is the connection here? I don’t think it creates a connection every time I make a query doesn’t it?
Am I missing something?
Is there some state stored under the otp_app identifier?

gregvaughn

gregvaughn

A Repo is a GenServer. There’s macro sugar to make this less obvious. GenServers can have state. In this case there is a connection pool involved.

LostKobrakai

LostKobrakai

The Repo itself is actually a Supervisor, not a GenServer. It starts a few things and the connection pool is one of those.

Where Next? Top

Trending in Questions Top

axelson
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
yasoob
Hi all! I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Liam_or
I don’t suppose you can lazy load a library depending on runtime data? For example if it’s a certain user they can lazy load ‘library A’ ...
New
RicoTrevisan
Hi all, I’m trying to get Pi working well as an external agent inside Tidewave.ai via ACP. Tidewave’s settings make it possible to add ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
PJUllrich
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews