How to add a session in page LiveView?

Hi guys, i want to use a session to do a shopping cart but i’m in LiveView Page like this:
conn |> put_session(cart: cart) but conn is not available in LiveView Page so what can I do?

You can’t. LV does not issue an HTTP request after the first load, so your plug pipeline is not run, so you can’t update session.

A simple thing to do is to add a hook to an element, then listen to some event in that hook and send an HTTP request when the event is received on client. Then send this event on server when appropriate. This is kind of hacky, but works.

Also, note that the updated session is not available in current LV. You do have new values, but if you certainly need the new session you must reload the page.

1 Like

Okay, thanks @vfsoraki, I’ll find a way to do that.

Thank you