On Tue, Dec 20, 2016 at 2:00 PM, Kakar Nyori <[email protected]> wrote:
> I am trying to grasp the concept of persistant data in django channels > <https://channels.readthedocs.io/en/stable/getting-started.html#persisting-data>. > 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_sessiondef 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_sessiondef 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_sessiondef 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 -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAFwN1ur2VLkxnT1jMbXmK1Y8UWN-7zm-Cio7CGT0h6b3e80Cmw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

