taoensso / sente

Realtime web comms library for Clojure/Script
https://www.taoensso.com/sente
Eclipse Public License 1.0
1.74k stars 193 forks source link

How to handle sente connection on expired session #330

Closed zalky closed 11 months ago

zalky commented 5 years ago

Hi, thanks for putting together this fantastic library! I've really loved working with sente so far and it has been a boon for development.

I have a working implementation that deals with user login/authentication/authorization/logout. Everything works, but I'd like to validate the method I used for dealing with active sente connections after the user session has been expired.

As best I can tell, the session data available to the sente event handler is that of the original request that initiated the handshake. All future sente events will see this original request with its corresponding session data, even if the user session has since changed or been expired.

I've placed some middleware on the sente event handler that checks the session store for the up-to-date session information and drops the request if the session has expired (optionally triggering some workflow client-side for logout/login, but that is beyond the scope of the question). I would ideally like to close the connection, rather than just drop the request, but the only guaranteed way I know to close the connection is the [:chsk/close] event, which is not part of the official api, and is subject to change.

So:

  1. Is there some easier approach to ensure sente connections do not remain active after user session has been expired?
  2. Is [:chsk/close] the only guaranteed way to close the connection server side?
ptaoussanis commented 11 months ago

@zalky Hi there, apologies for the very long delay replying!

To answer your questions:

  1. There's no built-in way to handle this, your approach sounds reasonable 👍
  2. Yes, that is correct 👍 [:chsk/close] is appropriate for this case, and will continue to be supported. It'll become officially documented in a future release.