I have been happily using Django-Channels for several months now. However, I went to add a second websocket dependent application to my Django project and I am running into trouble.
The error I am getting is websocket connection failed websocket is closed before the connection is established
. What is odd is that the first application was working before the second application was deployed. Further, the first application continues to work so long as the second application is not running.
The Django Channels documentation says:
Channels routers only work on the scope level, not on the level of individual events, which means you can only have one consumer for any given connection. Routing is to work out what single consumer to give a connection, not how to spread events from one connection across multiple consumers.
I think this means that Django-Channels does not support routing for multiple websocket connections. That is, I think I am trying to use the same websocket connection/port for two different applications. My routing.py
file looks as follows:
application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(
URLRouter([
path("first_application/stream/", app_1_consumers.AsyncApp1),
path("second_application/stream/", app_2_consumers.AsyncApp2),
])
)
})
When I attempted to use the setup below, it could not find the path to the first application:
application = ProtocolTypeRouter({
"websocket": AuthMiddlewareStack(
URLRouter([
path("second_application/stream/", app_2_consumers.AsyncApp2),
])
),
"websocket02": AuthMiddlewareStack(
URLRouter([
path("first_application/stream/", app_1_consumers.AsyncApp1),
])
),
})
How can I setup my Django application to serve up two different websocket connections using Django-Channels? Is it possible? Or am I just configuring things improperly?