Replies: 2 comments 6 replies
-
Hi @sevdog — thanks for this. My initial leaning would be to something like a the consumer setting up a task to periodically run I'm not sure I'm seeing the Why for the |
Beta Was this translation helpful? Give feedback.
-
Hi @carltongibson. I tried to figure out if there was in async def periodic(time):
while True:
# do stuff
...
await asyncion.sleep(time) But calling this in In an other scenario I would have used something different (ie: celery), but in this case I see these constraints:
So I belive that it would fit better if this kind of task is handled by something "close to" the consumer, also to have an implementation which does not requires an extra dependency which is not in the scope of the current library. For what I was able to find there is nothing in I also think that to have this work within channels the mechanism should not give any problem to the Maybe the same mechanism may be achieved by using a dedicated consumer to handle session related validation and which then will send specific messages to the Websocket consumer, but this seems too far complex for what I want to achieve (also it would require further logic to handle what is going to be an other application to handle session...). If anyone has a better solution I will be glad to listen and discuss. |
Beta Was this translation helpful? Give feedback.
-
Problem definition
I was looking for a way to handle session expiration or invalidation (ie: the user changed password). I tried to figure out how this is handled in other frameworks.
Sprint (Java)
In Spring the HTTP session can be used also in websocket connections and the session engine handles by itself expiration or logoff via an event-driven mechanism. The websocket can be setup to intercept these events to close connections server-side.
Ref: https://docs.spring.io/spring-session/reference/guides/boot-websocket.html
Socket.io (JavaScript)
socket.io does not handle the session by itself, relying on other libraries which implements the HTTP application server to handle it. However they suggest to use a
setInterval
(aka: periodic task) to check for session expiry.Ref: https://socket.io/how-to/use-with-express-session#handling-session-expiration
ASP.Net (C#)
Microsoft has defined an authentication layer which can be configured to "enable authentication expiration tracking which will close connections when a token expires".
Ref: https://learn.microsoft.com/en-us/aspnet/core/signalr/configuration?view=aspnetcore-7.0&tabs=dotnet#advanced-http-configuration-options
Possible solution
Based on those solutions I belive that it may be useful to have a kind of healthcheck which can be configured for websocket consumers.
The healthcheck should be a periodic task with the following specs:
websocket.accept
)websocket.close
andwebsocket.disconnect
)websocket.close
)Implementation
This package (which I belive is based on this SO suggestion) which defines a class which should handle a periodic task with asyncio, the problem is that it is not tested on any supported python version but 3.7. It may be forked/derived to have this kind of support in channels.
The
AsyncWebsocketConsumer
/WebsocketConsumer
may then be updated to perform the above methioned checks in theaccept
,close
andwebsocket_disconnect
methods only if an healthcheck is defined.Beta Was this translation helpful? Give feedback.
All reactions