tadasajon

tadasajon

`get_session` vs `fetch_session` -- why?

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.

Most Liked

tadasajon

tadasajon

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.

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.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement