From c1f801a20eb89ed422669c2266a1ae58ee33dd6f Mon Sep 17 00:00:00 2001 From: Artem Malyshev Date: Wed, 5 Apr 2017 13:41:30 +0300 Subject: [PATCH] Improve docs. (#589) --- docs/getting-started.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/getting-started.rst b/docs/getting-started.rst index 422e5dd..acef373 100644 --- a/docs/getting-started.rst +++ b/docs/getting-started.rst @@ -625,6 +625,8 @@ have a ChatMessage model with ``message`` and ``room`` fields:: # Save room in session and add us to the group message.channel_session['room'] = room Group("chat-%s" % room).add(message.reply_channel) + # Accept the connection request + message.reply_channel.send({"accept": True}) # Connected to websocket.receive @channel_session @@ -640,6 +642,19 @@ have a ChatMessage model with ``message`` and ``room`` fields:: def ws_disconnect(message): Group("chat-%s" % message.channel_session['room']).discard(message.reply_channel) +Update ``routing.py`` as well:: + + # in routing.py + from channels.routing import route + from myapp.consumers import ws_connect, ws_message, ws_disconnect, msg_consumer + + channel_routing = [ + route("websocket.connect", ws_connect), + route("websocket.receive", ws_message), + route("websocket.disconnect", ws_disconnect), + route("chat-messages", msg_consumer), + ] + Note that we could add messages onto the ``chat-messages`` channel from anywhere; inside a View, inside another model's ``post_save`` signal, inside a management command run via ``cron``. If we wanted to write a bot, too, we could put its