Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Django channels session issue with Redis as channel layer backend

I use Django Channels in combination with a seperate Javascript frontend. Websockets work fine. What I now want to do is:

  • when user opens website, backend generates a specific id. The backends saves that id in the session with message.channel_session['my_id']
  • on subsequent calls to the backend via the websocket, I want to retreive that key from the session.

However, sometimes this seems to work, and sometimes I get a KeyError; my_id does not seem to exist. My code:

routing.py:

from channels.routing import route
from chat.consumers import ws_connect, ws_receive

channel_routing = [
    route("websocket.connect", ws_connect),
    route("websocket.receive", ws_receive),
]

And consumers.py:

import uuid
from channels.sessions import channel_session


@channel_session
def ws_connect(message):
    message.channel_session['my_id'] = str(uuid.uuid4())

@channel_session
def ws_receive(message):
    my_id = message.channel_session['my_id']  # this one sometimes fails...

However, when I switch channel layer backend from Redis to in-memory, it works fine (I use ./manage.py runserver so I don't run a seperate worker)

Any ideas? Is there a race condition or something?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!