Persisting user data between live sockets

Hi :slight_smile:

I’m trying to wrap my head around the following concept -

Let’s suppose there’s a config dropdown a user selects (the options are dev & prod).

I want to ‘remember’ his selection throughout the session.
When I store it on the socket, it elapses every time the user interacts with the UI and I want to persist it.

I’ve seen a solution using LocalStorage / SessionStorage, but I couldn’t find a way of accomplishing that on the session itself.

What’s the recommended way of achieving that in Phoenix LiveView?

The user that is doing this action is logged in, so I have his id.
(I would love to know what could be done for anonymous users as well, but known users are more important here)

Thanks.

It depends, do you want to store that option on frontend only? I would guess most probably not.

Store it in a database? If you don’t need it to be persistent, you can store it in a genserver or ETS table, whatever fits your needs.

As for anonymous users, you could generate a cookie with a randomly generated identifier for their session, but since I don’t know what you want to do with that, the implementation might get tricky.