django channels on heroku hosting

Viewed 1141

Since I cant use redis I used the other channel layer that is InMemoryChannelLayer

WSGI_APPLICATION = 'django_analytics.wsgi.application'
ASGI_APPLICATION = "django_analytics.routings.application"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer",
        "CONFIG": {
        },
    },
}

I deployed it, on heroku but I am getting error

(index):9 WebSocket connection to 'wss://mkmnim.herokuapp.com/ws/chat/page/' failed: Error during WebSocket handshake: Unexpected response code: 404

I got it working at my local server but I am not able to do the same in hosting it online,Also will I have to do something related to daphne ? Initially I used redis and ran redis server locally but while hosting redis setup was paid so I choose InMemoryChannelLayer (channels without channel layer or any other free hosting)

so what should I do ?

django_analytics.routings.py

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from view_analytics import routing

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

view_analytics.routings.py

from django.conf.urls import url

from . import consumers

websocket_urlpatterns = [
    url(r'^ws/chat/room/$', consumers.ChatConsumer),
    url(r'^ws/chat/page/$', consumers.PageConsumer),

]

page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    let chatSocket = new WebSocket(
        'wss://' + window.location.host +
        '/ws/chat/' + 'page' + '/');

    chatSocket.onmessage = function(e) {
        const data = JSON.parse(e.data);
        const message = data['message'];
        console.log("message is"+ message)
    };

    chatSocket.onclose = function(e) {
        console.error('Chat socket closed unexpectedly');
    };
</script>

</body>
</html>

edit 1: added heroku logs as well

2018-11-13T08:12:14.240908+00:00 heroku[router]: at=info method=GET path="/view_analytics/page_check/" host=mkmnim.herokuapp.com request_id=93802d36-fece-47b0-9e91-7f453d891f00 fwd="47.31.181.82" dyno=web.1 connect=1ms service=60ms status=200 bytes=726 protocol=https 2018-11-13T08:12:14.240275+00:00 app[web.1]: 10.41.198.116 - - [13/Nov/2018:08:12:14 +0000] "GET /view_analytics/page_check/ HTTP/1.1" 200 536 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36" 2018-11-13T08:12:15.342043+00:00 heroku[router]: at=info method=GET path="/ws/chat/page/" host=mkmnim.herokuapp.com request_id=c2f225eb-1f88-4417-87fe-281b6dd6fd51 fwd="47.31.181.82" dyno=web.1 connect=1ms service=7ms status=404 bytes=2534 protocol=https 2018-11-13T08:12:15.340615+00:00 app[web.1]: Not Found: /ws/chat/page/ 2018-11-13T08:12:15.341106+00:00 app[web.1]: 10.168.81.123 - - [13/Nov/2018:08:12:15 +0000] "GET /ws/chat/page/ HTTP/1.1" 404 2351 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36"

0 Answers
Related