Django sessions updated at django channel consumers

Hi there,
I am trying to update a certain Django request session value in my Django channel consumer.
from my understanding from channels documentation, it’s the same session object and my session is updated correctly when I change its value in the consumer. however, when I reset my session value on the view side the value remains unchanged at the consumer session object.
I could be missing something very simple. or my understanding of how this works is faulty.

here is the snippet where I update at the
self.scope['session']['unr_msg'] = f"{int(self.scope['session']['unr_msg']) + 1}"


and at :
# reset session unread msgs count.
request.session['unr_msg'] = 0;
request.session.modified = True

I appreciate any help on this :slight_smile:

Hi AlaaKho,

I believe your understanding of sessions with Django Channels is correct.

When you say “the value remains unchanged at the consumer session object.” do you mean unr_msg is always the same value? If so, and that value is zero, I would try to verify that you’re not resetting the value to zero too often.

If you’re seeing the reset unr_msg to zero part not work, I would verify that part of the view gets executed either by using a debugger or a print statement. If it is getting called, try setting a different session element and verify that is persisted across requests. If that works and you can’t find that new session variable in a consumer, then there may be a bug in Django/Channels.

Hi @CodenameTim,
Thanks for your reply,

Just to clarify myself further,

  • session[‘unr_msg’] is a session value that keeps track of the unread messages in the user chat.

  • Once the user consumer receives an event of a message from WebSocket, I update self.scope['session']['unr_msg'] with the above code,

  • later when the user expands the chat window i.e. reads the msg, I send an ajax request to the server and reset the request.session['unr_msg'] to zero in my ajax view.

I have used print to debug, and what I am facing is that:

  • self.scope['session']['unr_msg'] keeps incrementing at the consumer side, and doesn’t pick up and changes at the views.

  • request.session['unr_msg'] however at the view side gets updated when we increment at the consumer side.

thus, if the user doesn’t read the latest message, and reload the page, the request.session['unr_msg'] reflects the value of the self.scope['session']['unr_msg'] that never picked up the reset.

I hope this made the problem clearer. I’ll try what you suggested and see what happens.

Many thanks!

and Yes, I have tried at the specific view called to set a new session element (which is printed at the view in request.session.keys()),
however when printing self.scope['session'].keys() at the consumer side after setting the new session key, the new element key doesn’t exist.
it seems like only when the new element is set BEFORE the WebSocket connection will it be recognized not in the midst.

Is it fair to claim this is channels problem?