No path found on Django Channels

Viewed 1352

I created a simple consumer on my Django channels application, but when i try to connect to the websocket from my frontend, i keep getting the following error:

ws_protocol: ERROR - [Failure instance: Traceback: <class 'ValueError'>: No route found for path 'messages/127.0.0.1:8000/messages/'.

Here is my routing: myapp>routing.py

from .consumers import EchoConsumer

websocket_urlpatterns = [
    path("messages/", EchoConsumer),
]

mysite>routing.py

# mysite/routing.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import myapp.routing

application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            myapp.routing.websocket_urlpatterns
        )
    ),
})

And here is how i'm trying to connect to the websocket from my frontend:

var wsStart = 'ws://' + window.location.host + window.location.pathname

Can anyone help me find what i'm doing wrong, please?

1 Answers

You are using path instead of url, just try this in routing.py:

from .consumers import EchoConsumer
from django.conf.urls import url

websocket_urlpatterns = [
    url("messages/", EchoConsumer),
]
Related