Understanding django channels persistant data

143 views
Skip to first unread message

Kakar Nyori

unread,
Dec 20, 2016, 9:01:20 AM12/20/16
to django...@googlegroups.com

I am trying to grasp the concept of persistant data in django channels. I have searched the reference and other parts but didn't clear my confusion. I would really appreciate if you help me understand better.

consumers.py:

@channel_session
def ws_connect(message):
    # Work out room name from path (ignore slashes)
    room = message.content['path'].strip("/")
    # Save room in session and add us to the group
    message.channel_session['room'] = room
    Group("chat-%s" % room).add(message.reply_channel)

Q.1 In the above line message.channel_session['room'], is the room another property of the message? Like it tells which session the message belongs to?

@channel_session
def ws_message(message):
    Group("chat-%s" % message.channel_session['room']).send({
        "text": message['text'],
    })

Q2. In the line Group("chat-%s" % message.channel_session['room']).send({, why are we not using the room variable like in the ws_connect() function, but instead we are using the channels_session?

@channel_session
def ws_disconnect(message):
    Group("chat-%s" % message.channel_session['room']).discard(message.reply_channel)

Q3. Same like above, why are we not using the room variable to discard from the group? Also is it not neccesary to remove the room from the session?

Andrew Godwin

unread,
Dec 20, 2016, 9:32:44 AM12/20/16
to django...@googlegroups.com
On Tue, Dec 20, 2016 at 2:00 PM, Kakar Nyori <nyori...@gmail.com> wrote:

I am trying to grasp the concept of persistant data in django channels. I have searched the reference and other parts but didn't clear my confusion. I would really appreciate if you help me understand better.

consumers.py:

@channel_session
def ws_connect(message):
    # Work out room name from path (ignore slashes)
    room = message.content['path'].strip("/")
    # Save room in session and add us to the group
    message.channel_session['room'] = room
    Group("chat-%s" % room).add(message.reply_channel)

Q.1 In the above line message.channel_session['room'], is the room another property of the message? Like it tells which session the message belongs to?

channel_session is like a normal session in that you can assign to anything - here, it just assigns to a key called `room`. There's nothing special about it, it's just a string that was decoded from the path. It could have been called `room_name`, for example, if the other code was updated to match.
 
@channel_session
def ws_message(message):
    Group("chat-%s" % message.channel_session['room']).send({
        "text": message['text'],
    })

Q2. In the line Group("chat-%s" % message.channel_session['room']).send({, why are we not using the room variable like in the ws_connect() function, but instead we are using the channels_session?


The `room` variable is out of scope now - this is a different function, potentially running on a different server, so you have to use the session to persist data (such as room).
 
@channel_session
def ws_disconnect(message):
    Group("chat-%s" % message.channel_session['room']).discard(message.reply_channel)

Q3. Same like above, why are we not using the room variable to discard from the group? Also is it not neccesary to remove the room from the session?


See my answer to Q2, and no, sessions are cleaned up like HTTP sessions are.

Andrew 

Kakar Nyori

unread,
Dec 20, 2016, 10:18:57 AM12/20/16
to Django users
Wow, from the creator himself! Thank you.

Just so we're clear, message.channel_session['key'] is the same as request.session['key'], did I get you correctly?

And there's just this another confusion, why can't we us "text": message.content['text']but instead its "text": message['text']?

Thank you
Kakar Nyori

Andrew Godwin

unread,
Dec 20, 2016, 11:21:43 AM12/20/16
to django...@googlegroups.com
On Tue, Dec 20, 2016 at 3:18 PM, Kakar Nyori <nyori...@gmail.com> wrote:
Wow, from the creator himself! Thank you.

Just so we're clear, message.channel_session['key'] is the same as request.session['key'], did I get you correctly?

It's very similar - the difference is that `request.session` lasts for the user's entire session length (which can be months), whereas `channel_session` just lasts for the lifetime of a single open WebSocket. It's a place to put socket state, since you can't use local variables.
 

And there's just this another confusion, why can't we us "text": message.content['text']but instead its "text": message['text']?

Both are valid; there's a shortcut for message.__getitem__ that just calls message.content.__getitem__.

Andrew
 

Kakar Nyori

unread,
Dec 20, 2016, 3:57:19 PM12/20/16
to Django users
Hi,

Yes that was it. Although I was getting this error:
ERROR - worker - Error processing message with consumer myproject.consumers.ws_message:

But this was corrected after the enforce_ordering was added into the consumer function. Thank you so much for your help and for introducing such a great tool for us.

Kakar Nyori
Reply all
Reply to author
Forward
0 new messages