Search code examples
pythondjangowebsocketdjango-channels

can we integrate websockets (ws) in our current project which only uses http.Can websockets and http can work together in one project .DJANGO CHANNELS


I created a social network site using django and my project or site was only using django with http protocols. I only have crud like features in my site but now i discovered about django channels. Django Channels allow django project to not only handle http but can also work with websockets protocol. So can we integrate websockets protocol in the website which only uses http ?. Can http and websockets protocol can work together in one project or one website ?. Can we integrate django channels in the existing django project which only uses http protocol ?. Is any website only have to use one protocol ?

Hope you will help me in my journey. 😊

I tried using django channels and django in one project and it was working fine . Both http and websockets protocols was working fine but i want answer from a experience developer bcoz i don't have much knowledge about networking and protocols and i never used websockets before.


Solution

  • Yes they can, check the documentation

    application = ProtocolTypeRouter({
    
        "http": get_asgi_application(),
    
        "websocket": AllowedHostsOriginValidator(
            AuthMiddlewareStack(
                URLRouter([
                    path("chat/admin/", AdminChatConsumer.as_asgi()),
                    path("chat/", PublicChatConsumer.as_asgi()),
                ]))),})