Closed qpwo closed 2 years ago
Can the server be notified when the client has stopped listening? I could poll isConnected, but something like .onDisconnect(...) would be nice.
Session
extends from EventEmitter
and emits the disconnected
event when the connection is closed.
I want to keep track of which user is on which session, and persist that across browser refresh. How would you recommend I go about this? I could store it in an object. Should I use something
like express-session
?
There's a bit of overlapping terminology here which may be a bit confusing. From the getting started guide in the context of Better SSE, a session is simply a single open connection between a client and server, it does not persist between browser refreshes or different pages so you'll need to use your own mechanism for that.
In some libraries that use Better SSE, for example, I have seen a pattern of creating a map (just a simple object or Set
) with the user ID for the key and a dedicated Channel
as the value. Then whenever a user connects you lookup if there is a channel for their ID and then simply register the session with that channel.
This allows you to track whether the user is currently connected at all (simply check that Channel#activeSessions
is more than one (1)) and also works if the user opens multiple tabs at once as they can have multiple sessions for themselves in that one channel.
This is probably the way I'd go about it too, and yes, you'd need some way to track the session with a cookie or some other method which express-session
seems to be designed for.
Thank you I'll try this
I want to keep track of which user is on which session, and persist that across browser refresh. How would you recommend I go about this? I could store it in an object. Should I use something like
express-session
?Example:
Can the server be notified when the client has stopped listening? I could poll
isConnected
, but something like.onDisconnect(...)
would be nice.Great library btw!!