Channels - query_string

44 views
Skip to first unread message

Sgiath

unread,
Feb 1, 2017, 8:34:38 AM2/1/17
to Django users
Hi,
I am trying to use the query_string parameter and I want to ask what is the correct way how to use it.
I am using JsonWebsocketConsumer and when debugging I noticed that on the Handshake the message content contains path, headers, query_string, client, server, reply_channel and order.
But every other message contains just reply_channel, path, order and text
How should I correctly use it? Specifically I want authenticate user based on the query_string (I send token in it) I can do that in connection phase but what should I do next? On every other message the user is AnnonymousUser because there is no query_string. Should I save query_string into channel_session? Should I save user into channel_session?
BTW I cannot use user from http session because I am connecting to WS from the mobile app.

Thanks

Andrew Godwin

unread,
Feb 1, 2017, 1:01:47 PM2/1/17
to django...@googlegroups.com
Hi,
You're right - a lot of the information only appears in the first "connect" message. If you want to persist it, you can use a channel session: http://channels.readthedocs.io/en/stable/getting-started.html#persisting-data

This will let you save information (such as the token, or even a User object) into the session in the connect consumer, and then let you use it in other consumers that have the same decorator.

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/a30535e3-29e1-4547-af70-0e391d1b80d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sgiath

unread,
Feb 2, 2017, 3:48:36 AM2/2/17
to Django users
Thanks for explaining.
I have one problem: when I try to save User object to the channel_session I get exception: TypeError: Object of type 'User' is not JSON serializable.
What am I doing wrong? Should I somehow define how to JSON serialize the User object?


On Wednesday, February 1, 2017 at 7:01:47 PM UTC+1, Andrew Godwin wrote:
Hi,
You're right - a lot of the information only appears in the first "connect" message. If you want to persist it, you can use a channel session: http://channels.readthedocs.io/en/stable/getting-started.html#persisting-data

This will let you save information (such as the token, or even a User object) into the session in the connect consumer, and then let you use it in other consumers that have the same decorator.

Andrew

On Wed, Feb 1, 2017 at 5:34 AM, Sgiath <sgi...@sgiath.net> wrote:
Hi,
I am trying to use the query_string parameter and I want to ask what is the correct way how to use it.
I am using JsonWebsocketConsumer and when debugging I noticed that on the Handshake the message content contains path, headers, query_string, client, server, reply_channel and order.
But every other message contains just reply_channel, path, order and text
How should I correctly use it? Specifically I want authenticate user based on the query_string (I send token in it) I can do that in connection phase but what should I do next? On every other message the user is AnnonymousUser because there is no query_string. Should I save query_string into channel_session? Should I save user into channel_session?
BTW I cannot use user from http session because I am connecting to WS from the mobile app.

Thanks

--
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...@googlegroups.com.

Andrew Godwin

unread,
Feb 2, 2017, 12:54:52 PM2/2/17
to django...@googlegroups.com
Ah, yes, sorry, the default serializer in Django cannot serialise model instances as it's JSON-based: https://docs.djangoproject.com/en/1.10/topics/serialization/

You can try switching to an alternate serialiser like pickle which does (but I don't recommend this), or just put the ID of the user into the session and then re-fetch it later on.

Andrew

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.
Reply all
Reply to author
Forward
0 new messages