question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to combine the Django WebSockets "websocket_urlpatterns" (path) of multiple apps in the Project URLRouter?

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
jules-chcommented, Oct 14, 2021

It should work with :

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import App2.routing
import App1.routing 
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProject.settings")

application = ProtocolTypeRouter({
  'websocket': AuthMiddlewareStack(
    URLRouter([
    *App1.routing.websocket_urlpatterns,
    *App2.routing.websocket_urlpatterns,
    ])
  )
})

It essentially joins those 2 lists to create the URLRouter.

4reactions
MuhammadBilal1commented, Oct 15, 2021

It should work with :

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import App2.routing
import App1.routing 
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProject.settings")

application = ProtocolTypeRouter({
  'websocket': AuthMiddlewareStack(
    URLRouter([
    *App1.routing.websocket_urlpatterns,
    *App2.routing.websocket_urlpatterns,
    ])
  )
})

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found