How to combine the Django WebSockets "websocket_urlpatterns" (path) of multiple apps in the Project URLRouter?
See original GitHub issueI have multiple apps and they all are using the “WebSockets (routing.py)”, and I have routing.py in my project and apps.
I can only add the websocket_urlpatterns
of single app in my URLRouter
of my project, if I try to add the other “websocket_urlpatterns” in the URLRouter, it didn’t worked and I get the error.
Project routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import django_chatter.routing
import app1.routing
import os
from time import sleep
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myProject.settings")
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
URLRouter(
app1.routing.websocket_urlpatterns # send request to chatter's urls
)
)
})
App1 routing.py
from django.urls import path
from django_chatter import consumers
import app1.consumers
websocket_urlpatterns = [
path('ws/route1/' , app1.consumers.Consumer1.as_asgi()),
path('ws/route2/' , app2.consumers.Consumer2.as_asgi()),
path('ws/route3/' , app3.consumers.Consumer3.as_asgi()),
]
App2 routing.py
from django.urls import path
from . import consumers
websocket_urlpatterns = [
path('ws/chatter/chatrooms/<str:room_uuid>/', consumers.ChatConsumer.as_asgi()),
path('ws/chatter/users/<str:username>/', consumers.AlertConsumer.as_asgi())
]
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
How to use multiple websocket connections ... - Stack Overflow
The tutorial simply points to chat.routing.websocket_urlpatterns which is a list of paths or urls directly. You can concatenate these lists to ...
Read more >Django Channels and WebSockets - LogRocket Blog
Learn how to use Django Channels and WebSockets, a new protocol that provides full-duplex communication, by building a real-time game app.
Read more >How to use multiple websocket connections using Django ...
The tutorial simply points to chat.routing.websocket_urlpatterns which is a list of paths or urls directly. You can concatenate these lists to build your ......
Read more >Django Channels & WebSockets Oversimplified - YouTube
An over simplified real time chat application using Django channels and websockets. Django Complete Course: ...
Read more >Routing — Channels 4.0.0 documentation
Channels provides routing classes that allow you to combine and stack your ... ProtocolTypeRouter as the root application of your project - the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It should work with :
It essentially joins those 2 lists to create the URLRouter.
Thank you. It worked. Astrisk before the app name is the key here.
I tried this solution without Astrisk before the app name and it didn’t worked. But now I put the asktrisk and it’s working.