mirror of
https://github.com/django/daphne.git
synced 2025-07-13 09:22:17 +03:00
Update docs to recommend doing routing not in settings
This commit is contained in:
parent
655213eff9
commit
4a8bae272b
|
@ -65,9 +65,9 @@ you can write a function to consume a channel, like so::
|
||||||
def my_consumer(message):
|
def my_consumer(message):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
And then assign a channel to it like this in the channel backend settings::
|
And then assign a channel to it like this in the channel routing::
|
||||||
|
|
||||||
"ROUTING": {
|
channel_routing = {
|
||||||
"some-channel": "myapp.consumers.my_consumer",
|
"some-channel": "myapp.consumers.my_consumer",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,21 +44,25 @@ For now, we want to override the *channel routing* so that, rather than going
|
||||||
to the URL resolver and our normal view stack, all HTTP requests go to our
|
to the URL resolver and our normal view stack, all HTTP requests go to our
|
||||||
custom consumer we wrote above. Here's what that looks like::
|
custom consumer we wrote above. Here's what that looks like::
|
||||||
|
|
||||||
|
# In settings.py
|
||||||
CHANNEL_BACKENDS = {
|
CHANNEL_BACKENDS = {
|
||||||
"default": {
|
"default": {
|
||||||
"BACKEND": "channels.backends.database.DatabaseChannelBackend",
|
"BACKEND": "channels.backends.database.DatabaseChannelBackend",
|
||||||
"ROUTING": {
|
"ROUTING": "myproject.routing.channel_routing",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
# In routing.py
|
||||||
|
channel_routing = {
|
||||||
"http.request": "myproject.myapp.consumers.http_consumer",
|
"http.request": "myproject.myapp.consumers.http_consumer",
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
As you can see, this is a little like Django's ``DATABASES`` setting; there are
|
As you can see, this is a little like Django's ``DATABASES`` setting; there are
|
||||||
named channel backends, with a default one called ``default``. Each backend
|
named channel backends, with a default one called ``default``. Each backend
|
||||||
needs a class specified which powers it - we'll come to the options there later -
|
needs a class specified which powers it - we'll come to the options there later -
|
||||||
and a routing scheme, which can either be defined directly as a dict or as
|
and a routing scheme, which points to a dict containing the routing settings.
|
||||||
a string pointing to a dict in another file (if you'd rather keep it outside
|
It's recommended you call this ``routing.py`` and put it alongside ``urls.py``
|
||||||
settings).
|
in your project.
|
||||||
|
|
||||||
If you start up ``python manage.py runserver`` and go to
|
If you start up ``python manage.py runserver`` and go to
|
||||||
``http://localhost:8000``, you'll see that, rather than a default Django page,
|
``http://localhost:8000``, you'll see that, rather than a default Django page,
|
||||||
|
@ -78,13 +82,8 @@ serve HTTP requests from now on - and make this WebSocket consumer instead::
|
||||||
|
|
||||||
Hook it up to the ``websocket.connect`` channel like this::
|
Hook it up to the ``websocket.connect`` channel like this::
|
||||||
|
|
||||||
CHANNEL_BACKENDS = {
|
channel_routing = {
|
||||||
"default": {
|
|
||||||
"BACKEND": "channels.backends.database.DatabaseChannelBackend",
|
|
||||||
"ROUTING": {
|
|
||||||
"websocket.connect": "myproject.myapp.consumers.ws_add",
|
"websocket.connect": "myproject.myapp.consumers.ws_add",
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Now, let's look at what this is doing. It's tied to the
|
Now, let's look at what this is doing. It's tied to the
|
||||||
|
@ -116,12 +115,10 @@ group it's not in)::
|
||||||
Of course, this is exactly the same code as the ``connect`` handler, so let's
|
Of course, this is exactly the same code as the ``connect`` handler, so let's
|
||||||
just route both channels to the same consumer::
|
just route both channels to the same consumer::
|
||||||
|
|
||||||
...
|
channel_routing = {
|
||||||
"ROUTING": {
|
|
||||||
"websocket.connect": "myproject.myapp.consumers.ws_add",
|
"websocket.connect": "myproject.myapp.consumers.ws_add",
|
||||||
"websocket.keepalive": "myproject.myapp.consumers.ws_add",
|
"websocket.keepalive": "myproject.myapp.consumers.ws_add",
|
||||||
},
|
}
|
||||||
...
|
|
||||||
|
|
||||||
And, even though channels will expire out, let's add an explicit ``disconnect``
|
And, even though channels will expire out, let's add an explicit ``disconnect``
|
||||||
handler to clean up as people disconnect (most channels will cleanly disconnect
|
handler to clean up as people disconnect (most channels will cleanly disconnect
|
||||||
|
@ -152,18 +149,13 @@ any message sent in to all connected clients. Here's all the code::
|
||||||
def ws_disconnect(message):
|
def ws_disconnect(message):
|
||||||
Group("chat").discard(message.reply_channel)
|
Group("chat").discard(message.reply_channel)
|
||||||
|
|
||||||
And what our routing should look like in ``settings.py``::
|
And what our routing should look like in ``routing.py``::
|
||||||
|
|
||||||
CHANNEL_BACKENDS = {
|
channel_routing = {
|
||||||
"default": {
|
|
||||||
"BACKEND": "channels.backends.database.DatabaseChannelBackend",
|
|
||||||
"ROUTING": {
|
|
||||||
"websocket.connect": "myproject.myapp.consumers.ws_add",
|
"websocket.connect": "myproject.myapp.consumers.ws_add",
|
||||||
"websocket.keepalive": "myproject.myapp.consumers.ws_add",
|
"websocket.keepalive": "myproject.myapp.consumers.ws_add",
|
||||||
"websocket.receive": "myproject.myapp.consumers.ws_message",
|
"websocket.receive": "myproject.myapp.consumers.ws_message",
|
||||||
"websocket.disconnect": "myproject.myapp.consumers.ws_disconnect",
|
"websocket.disconnect": "myproject.myapp.consumers.ws_disconnect",
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
With all that code in your ``consumers.py`` file, you now have a working
|
With all that code in your ``consumers.py`` file, you now have a working
|
||||||
|
|
Loading…
Reference in New Issue
Block a user