Fork me on GitHub
#hyperfiddle
<
2024-07-08
>
braai engineer08:07:08

Persistent Session Question: What's the easiest way in Electric to identify a user across websocket session keys without making them sign up or making them navigate away from the main Electric endpoint, e.g. to /auth? Maybe a token in localStorage? Not for security-sensitive app – just to identify users for about 30 minutes in case they lock their phone and websocket dies & reconnects. Should I generate an auth token on server, store in localStorage on client & pass it in on component mount?

braai engineer09:07:39

Put differently, is it possible to store a username cookie from Electric without reloading the page?

braai engineer09:07:32

Oh cool, I see I can set a cookie from JS, and it shows up on subsequent load in :cookies (never used JS-side cookies before):

(defn set-cookie! [key value]
     ; do we need to do some URI encoding here?
     (set! (.-cookie js/document) (str key "=" value ";path=/; max-age=86400")))

Tommy Jolly09:07:02

I'm also working on party games. I use localStorage myself, but cookies have the advantage (or disadvantage) that they can expire after a certain time.

Tommy Jolly10:07:37

I store their first websocket ID in localStorage, then use that as a user ID

👍 1
grounded_sage13:07:14

You can make an ID and put it in localstorage and a map on the server. I used this for a chat to get messages of the chat in realtime across tabs and server side streaming of AI generated text across all tabs.