Handle changing users (e.g. a logout) in a scope/WebSockets

11 views
Skip to first unread message

Daniel Gilge

unread,
Feb 19, 2018, 1:59:16 AM2/19/18
to Django users
Packages: Channels 2.0, Django 2.0

Hi,

When I understand it correctly a scope’s user object isn’t updated when a user changes (e.g. logs in or out in another window) for the whole lifetime of the scope. (Please correct me if I am wrong.) Now this can be a serious issue as you can imagine.

What are best practices to deal with that?

My ideas so far:

Logout

I think that’s quite easy:
I insert the channel name during connection to a list which is linked to the current user. (I’ll probably store that with redis.) I listen to the user_logged_out signal and close all channels of the user after a logout (using the list).

Login

I think I’ll do the same here because the JavaScript library should try to reconnect anyway. Then I had a new consumer instance with a new user object. But I’m not sure if this is a good practice.

Alternatively I could update the user object of the scope/every consumer instance concerned. Maybe I could maintain a per user list containing types which perform the user object update. (But I don’t know if it’s read only or messes things up.)

Other cases

Procedere for is_active, is_anonymous and has_usable_password will be similar to login/logout.

Suggestions are welcome!

Andrew Godwin

unread,
Feb 19, 2018, 2:48:35 AM2/19/18
to django...@googlegroups.com
Correct, it's fixed at connection time, much like a Django request object. If you want to vary what user it is, I suggest you just reconnect the websocket when the user logs in or out to make sure you don't have stale data - you should be building for reconnection anyway as network environments will cause them.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/60789998-0b64-40a4-9531-6c6f2a168b7c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages