While it is not that complicated to write routes for the CHANNEL_LAYERS['ROUTING'] setting, the Django Admin and some 3rd party apps (like Django Rest Framework) generate routes to be included into a URLConf.
Is it possible to just hook up Django's URL resolving system into Channel's routing system? Like:
channel_routing = {
'http.request': DjangoURLConfConsumer(),
# Websocket stuff:
'websocket.connect': consumers.ws_connect,
'websocket.receive': consumers.ws_receive,
'websocket.disconnect': consumers.ws_disconnect,
}
where DjangoURLConfConsumer is a function/class that would load Django's URLConf and dispatch the message to the traditional (as in synchronous) views.
Maybe Channels already does this?
Yes, you can use the channels.handler.ViewConsumer to pass any http.request message into the view system, though this is done for you by default.
In addition, in the latest master code, the routing gains the ability to route to custom HTTP endpoints/WebSocket consumers based on url (path), and will always fall back to Django's view handling if nothing processes a http.request message.
(Generally, you don't want to route http.request in the routing and just leave it out so it does the default, which is exactly what you want. You only need to route it yourself if you're doing something unusually low-level)
Got it, thanks! I just didn't find the docs section stating that this already happens by default. Thanks for the quick reply!
Channels is looking really good this far. Perhaps explicitly add this to the starting section of the docs? I did not find it either so had to come here to find this out.
Good point, I thought I had. Added a paragraph in there.
Is it possible to process some of http requests by channels consumers and some of them by the traditional django views based on their url?
Actually I need to add a single endpoint that should be asynchronous - using consumers - and others are simple rest-framework views.
I thought this issue is related to my question but I couldn't find the channels.handler.ViewConsumer you mentioned here .
@itsmehdi97 Take a look at the example here: https://channels.readthedocs.io/en/stable/topics/routing.html#urlrouter
It covers the exact use-case you're asking about.