# Using multiple Asgi apps in a project with custom middleware

**URL:** <https://forum.djangoproject.com/t/using-multiple-asgi-apps-in-a-project-with-custom-middleware/31107>\
**Category:** Async/Channels\
**Created:** [May 13, 2024, 6:04am UTC](https://forum.djangoproject.com/t/using-multiple-asgi-apps-in-a-project-with-custom-middleware/31107 "2024-05-13T06:04:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [May 13, 2024, 6:04am UTC](https://forum.djangoproject.com/t/using-multiple-asgi-apps-in-a-project-with-custom-middleware/31107/1 "2024-05-13T06:04:30Z")

</div>

Hi,

I am trying to implement a Projects with multiple Apps, with different WebSocket endpoints.

The below code, obviously does not work, but its for reference on what i am intending to do.

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

```

The ` websocket_urlpatterns` is the projects internal WebSocket which is authenticated.  
while `live_websocket_urlpatterns` is from a third party socket providers, which i do not intend to use authentication.

is there any pointers where I could accomplish this.?

thank you.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [May 13, 2024, 6:17am UTC](https://forum.djangoproject.com/t/using-multiple-asgi-apps-in-a-project-with-custom-middleware/31107/2 "2024-05-13T06:17:38Z")

</div>

> [@matteyahmed](#):
>
> `URLRouter`

You should be able to use a `URLRouter` to branch to the two different `websocket` protocol handling apps, based on URL. (Otherwise you’d need to implement a custom routing middleware to use some other aspect of the request to route by.)

---

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [May 13, 2024, 7:23am UTC](https://forum.djangoproject.com/t/using-multiple-asgi-apps-in-a-project-with-custom-middleware/31107/3 "2024-05-13T07:23:29Z")

</div>

> [@carltongibson](#):
>
> `URLRouter` to branch to the two different `websocket` protocol

Thanks, I’ve tried it, however what I am trying to do is, set different WebSocket routing patterns for different paths.

for `websocket_urlpatterns` I am using a custom middleware `TokenAuthMiddleWare`,  
and for `live_websocket_urlpatterns` I do not want the custom middleware.

---

<div class="post-metadata">

**Author:** ![matteyahmed](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matteyahmed/32/11483_2.png) [@matteyahmed](https://forum.djangoproject.com/u/matteyahmed)\
**Post date:** [May 13, 2024, 7:56am UTC](https://forum.djangoproject.com/t/using-multiple-asgi-apps-in-a-project-with-custom-middleware/31107/4 "2024-05-13T07:56:19Z")

</div>

Hi,  
I think I managed to make it work, please check if the following snippet is a safe way to do the WebSocket routing:

```auto
# asgi.py
from api.routing import websocket_urlpatterns as api_app
from tracker.routing import live_websocket_urlpatterns as live_tracker
from api.middleware import TokenAuthMiddleWare

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

```

thanks.

---

<div class="post-metadata">

**Author:** ![Meenakshy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/meenakshy/32/29632_2.png) [@Meenakshy](https://forum.djangoproject.com/u/Meenakshy)\
**Post date:** [August 22, 2025, 5:36pm UTC](https://forum.djangoproject.com/t/using-multiple-asgi-apps-in-a-project-with-custom-middleware/31107/5 "2025-08-22T17:36:35Z")

</div>

Doesn’t it overwritethe application variable?
