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.
#project/apps.py
urlpatterns = [path("", include("project.urls"))]
#app/websockets/routing.py
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.