Django 3.0.1 has released for sometime. We still use Django 1.11 in one of our project, with Channels 1.1.8, I think it's the time to upgrade to newest Django and Channels.
According this upgrade guide, Channels 2 is a fully rewritten, lots of things has changed. This post is just a record about the upgrade I've done.
In Channels 2 you need to add an
ASGI_APPLICATION in the settings file, point to the asgi application.
routing.py file, add an
application variable in the file like the bellow.
websocket key means it will handle websocket requests, the
URLRouter just work as Django's.
MyConsumer is the handler for the request.
When a client connected, it will call
connect at first, you can do authentication(If you have your own auth method other than use Django's, otherwise you can use AuthMiddlewarestack instead), some preparing, if you didn't want to accept the connect, you can use
self.disconnect() to end the connection, it will also call
disconnect, where you can do some cleaning. The function
receive will be called when the client send messages to you.
Simple, huh? There are many other types of consumers, you can check here.
Passing through data in session
When a client connected, you may need to check the database and put some useful data in the session, so that you can use these data later. You have to use django session to do this in Channels 1, but in Channels 2 you didn't need to use django session, just store the data to
Of cause, you still can use django sessions to store data, check the doc here, just add the
SessionMiddlewareStack in the routing file.
self.scope["session"]["seed"] = '123' to set the session, and don't forget to use
self.scope["session"].save() to save the session if you want to persist.
You can use
self.send(text_data="") to send a message to a client. But if you need to send to multiple client at a same time, you need to use Channel Layers.
There are many backends you can use, I recommend you use
Then you need to create groups when user connect in
some_room is the group id you want the user to join, think about it"s just like a chat room, everyone who join this room is a group. Or you can use the
user_id as the group id like us, so you can send same messages to the same user at once even they have many sessions through many clients.
self.channel_layer.group_discard to quite a group and use
self.channel_layer.group_send to send a message to a group. If you need to send to the group outside the comsumer, just use the codes below to get the
Please beware that the message type here is
group.notify, it can be anything you want, just add a handler for it in the comsumer, like the function
group_notify I added above(replace the "." to "_").
Deploy and run
After add the
ASGI_APPLICATION, you can run the server use command
./manage.py runserver, the server will handle both http and websocket requests.
You can also use
daphne to run the server, just need to add an
asgi.py like the bellow.
Then run with command
daphne -p 8001 proj.asgi:application, it also can handle both http and websocket request.
If you prefer to handle http requests use WSGI method like before, just run
proj.asgi:application use wsgi server like