Difficulty trying to have websockets self-contained in application

I am trying to create a self-contained Django app that can be plugged in to another project that includes HTTP URLs and Websocket URLs. The issue is, I am trying to make it so that when someone imports my app into their INSTALLED_APPS they should be able to import both types of requests at once without making any changes to their asgi.py. Currently I am to import both types of URLs at once if I update my asgi.py to the following:

import app.websockets.routing as routing
# application = get_asgi_application() # not working
application = ProtocolTypeRouter(
        "http": get_asgi_application(),
        "websocket": AuthMiddlewareStack(URLRouter(routing.websocket_urlpatterns)),
) # working

However as I mentioned, I want it so that I do not have to make any changes to the asgi.py, just in urls.py the app/urls.py are imported.

urlpatterns = [path("", include("project.urls"))]
websocket_urlpatterns = [
    path("ws/sockets/", Consumer.as_asgi()), # works
    # path("sockets/", Consumer.as_asgi()), # not working
# app/urls.py
from app.http_urls import http_urls
# from app.websockets.routing import websocket_urlpatterns # doesn't work

urlpatterns = [
    path("vms/", include((http_urls, "http"))),
    # path("ws/", include((websocket_urlpatterns, "websockets"), namespace="ws")) # doesn't work

Is what I’m attempting to do even possible? I am basically trying to get the not working approach that I’m trying and that is commented out to work.

Not that I’m aware of, at least not that I can see from what I’m understanding of what you have posted here. (Side note: It’s difficult for me to follow your description with having the two situations intermixed. I would find it easier to understand if you posted the two versions separately - the one that works followed by the one that doesn’t.)

The asgi.py file seems to be very sensitive to the order in which things are done. (Side note: See the thread at I get the error "Apps aren't loaded yet." when publishing with Daphne to see one such conversation here about how picky it is.)

1 Like

Thank you for you response.
I see, and sorry for the formatting, I actually thought it would be more readable the way I did it to have a quick comparrison of what works and what doesn’t.

No worries, it’s just something to consider for future reference.

To give you an idea of why it tends to be confusing, consider the situation such as this:

I can’t tell from this layout whether the commented line is in addition to, or a replacement for, the original line.

Whereas, if you had those as two separate sections, it would be clear:

This works:

urlpatterns = [
    path("vms/", include((http_urls, "http"))),

This doesn’t

urlpatterns = [
    path("ws/", include((websocket_urlpatterns, "websockets"), namespace="ws")) # doesn't work

It’s more explicit what’s being tried each way.