0

I am trying to implement a Django Projects with multiple Apps, with multiple Consumers with different WebSocket routing patterns for each paths.

Below is my working code, the code works, but is obviously is not correct, I have tried with different methods as the Django Chennels Documents Django Channels - Routing states but never got it to work,

#asgi.py

from api.middleware import TokenAuthMiddleWare
from api.routing import websocket_urlpatterns
from live_tracker.routing import live_websocket_urlpatterns


application = ProtocolTypeRouter(
    {
        'http': get_asgi_application(),
        'websocket': TokenAuthMiddleWare(AuthMiddlewareStack(URLRouter(websocket_urlpatterns))),
    }
)
application = ProtocolTypeRouter(
    {
        'http': get_asgi_application(),
        'websocket': AuthMiddlewareStack(URLRouter(live_websocket_urlpatterns)),
    }
)

From the 2 apps I have, in first app I have made it so that a token is needed to access the websocket_urlpatterns while in the second app I only need default django authentication: live_websocket_urlpatterns.

I went through StackOverflows this question How to use multiple websocket connections using Django Channels? the question is handling AuthMiddlewareStack only,to which I have 2, a custom TokenAuthMiddleWare and django's default AuthMiddlewareStack

is there a way I can merge the 2 applictaion to 1 applications?

1 Answer 1

0

We can do it, but we cant use two asgi applications within one Django instance. For that, you'd need to be running another entire container with something like Docker or Kubernetes

So, you're going to have to make sure that neither your urls.py (for http connections) or your routing.py (for websocket connections) use the same paths.

For example, you have api.routing and live_tracker.routing and they need to each have their own unique paths for example "wss/connect_api" and "wss/connect_live_tracker".

Now you need to put all of your http paths in both urls.py into one file urls.py that is used by one asgi application. You can do the same with the websocket paths in both routing.py files, merge them into one file. If you want to keep api.routing and live_tracker.routing then you can try to get the urlpatterns and append them together before setting application.

Now in your asgi.py, make sure that you are creating application only once. In your settings.py, make sure that you have in your ALLOWED HOSTS both domains for both websites you intend to use.

Our Django application only knows to direct traffic by the path succeeding the url. For example, if you have a url path on domain1, called url1, you could still access it from domain2.

SAME PAGE

domain1.com/url1

domain2.com/url1

Now, this may not be a big issue for you, this is why we got rid of identical url paths on both domains. And if we dont ever give the user a link to go there, it shouldnt be a big problem... still. Its unprofessional.

One methodology to get around this for your sensitive pages, for example pages that require a logged in user, is to check cookies to see if they have logged in before and preform a redirect otherwise.

Here's a catch all that might be a bit tedious - use request.gethost() when the request actually hits views.py. And if your user has found themself on the wrong domain hitting the wrong url, go ahead and redirect them to the home page or another page of the correct url/domain.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply, I managed to solve this by, getting rid of the second application and using the first application, where I fetched the token in the view and forwarded it to the front end.
Right yeah, one application is the way to go, glad you were able to get it working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.