tadasajon

tadasajon

Here are the docs for Plug.Conn.get_session: Plug.Conn.html#get_session/1

And here is the key line from the docs:

Returns the whole session.

And here are the docs for Plug.Conn.fetch_session: Plug.Conn.html#fetch_session/2

And here is the key line:

Fetches the session from the session store. Will also fetch cookies.

Can anyone explain to me why there are two functions that appear to do almost the same thing? Why would I use one vs the other? What is the session store – why doesn’t get_session also get the session from the “session store”? Or does it?

Also, in a nutshell, what is the difference between a connection and a session? It seems to me like I should just be able to do something like conn.session – in fact, I don’t understand why there is something called a session if I already have a connection – why can’t any information stored in the session just be stored in the connection instead?

I don’t understand why there need to be these backflips between connections and sessions.

Showing Posts 1 to 5

LostKobrakai

LostKobrakai

Session store is the place the session data is stored in between separate http requests. This by default is a cookie, but could also be some server-side database.

fetch_session does retrieve the session data from the session store to put it onto the conn of your current web request.

get_session is the API to fetch session values from the set present on conn while handling a request

After you’re done handling a certain http request the session data on conn is put back into the session storage for persistance until the next http request.

tadasajon

tadasajon OP

Thanks! Maybe I’ll submit a PR to the Plug.Conn docs to make it clear that fetch_session does not return the session but rather returns the connection.

hauleth

hauleth

Well, it says exactly that in “Specs” section of the documentation of that function.

tadasajon

tadasajon OP

Yes, but not everyone reads specs fluently – a lot of people prefer plain English. So rather than sending people off to do a tutorial on how to read specs, it is better just to be clear, in my opinion.

derek-zhou

derek-zhou

The fact that fetch_session/2 is sparsely documented is also in part due to the fact that it is a plug and not a function you normally call yourself.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews