I use Django Channels in combination with a seperate Javascript frontend. Websockets work fine. What I now want to do is:
message.channel_session['my_id']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?