S
S
Sergey Ganzhela2018-03-07 10:13:17
Django
Sergey Ganzhela, 2018-03-07 10:13:17

How to beat Channels(group chat app) please?

Good afternoon, people of the forum, help to deal with the problem.
I wrote a group chat using (Django 1.10 Channels 1.8 Daphne 1.4 Redis-server)
and, oddly enough, it turned out to be deployed the first time the chat worked, tested 10-11 people, everything was normal, they decided to open it for everyone here everything started and the chat went down and after that already stopped working :(

WebSocket connection to 'wss://example.com/dashboard/chat/' failed: WebSocket is closed before the connection is established

my settings.py
CHANNEL_LAYERS = {
    "default": {
        # This example app uses the Redis channel layer implementation asgi_redis
        "BACKEND": "asgi_redis.RedisChannelLayer",
        'CONFIG': {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        }
        "ROUTING": "stp.routing.channel_routing",  # We will create it in a moment
    },
}

asgi.py
import os
import channels.asgi


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stp.settings')
channel_layer = channels.asgi.get_channel_layer()

routing.py in settings.py
from channels import include
channel_routing = [
    include('multichat.routing.chat_routing', path=r"^/dashboard/chat/"),
]

routing.py in app multichat
from channels.routing import route
from multichat.consumers import *
chat_routing = [
    route("websocket.connect", chat_connect),
    route("websocket.receive", chat_receive),
    route("websocket.disconnect", chat_disconnect)
]

Accordingly, in consumers.py, only what kind of connection is involved so that the code does not fence!
@channel_session_user_from_http
def chat_connect(message):
    logger.info('websocket_connect. message = %s', message)
    Group("all").add(message.reply_channel)
    Room.objects.add("all", message.reply_channel.name, message.user)
    message.reply_channel.send({"accept": True})

a piece of nginx
location /dashboard/chat/ {
            proxy_pass http://0.0.0.0:8001;
            proxy_http_version 1.1;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    }

I run runworker we get in the log ./manage.py runworker
./manage.py runworker --traceback
2018-03-06 18:28:10,456 - INFO - runworker - Using single-threaded worker.
2018-03-06 18:28:10,456 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer)
2018-03-06 18:28:10,457 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive

when starting daphne we have daphne -b 0.0.0.0 -p 8001 stp.asgi:channel_layer
daphne -b 0.0.0.0 -p 8001 stp.asgi:channel_layer
2018-03-06 18:29:57,498 INFO     Starting server at tcp:port=8001:interface=0.0.0.0, channel layer stp.asgi:channel_layer.
2018-03-06 18:29:57,499 INFO     HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-03-06 18:29:57,499 INFO     Using busy-loop synchronous mode on channel layer
2018-03-06 18:29:57,499 INFO     Listening on endpoint tcp:port=8001:interface=0.0.0.0
127.0.0.1:55384 - - [06/Mar/2018:18:30:23] "WSCONNECTING /dashboard/chat/" - -
127.0.0.1:55384 - - [06/Mar/2018:18:30:25] "WSDISCONNECT /dashboard/chat/" - -
127.0.0.1:55478 - - [06/Mar/2018:18:30:55] "WSCONNECTING /dashboard/chat/" - -
127.0.0.1:55478 - - [06/Mar/2018:18:30:57] "WSDISCONNECT /dashboard/chat/" - -
127.0.0.1:56066 - - [06/Mar/2018:18:31:27] "WSCONNECTING /dashboard/chat/" - -
127.0.0.1:56066 - - [06/Mar/2018:18:31:29] "WSDISCONNECT /dashboard/chat/" - -

redis-server is running!
but the campaign is somewhere between daphne and backend chanels because the logger does not reach consumers.py and does not display anything :( In general, the ambush will be grateful for any help!
Regards, Sergey.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mykola, 2018-03-07
@IKMOL

Do you have an SSL certificate (https) on your server? if so, then daphne needs to be launched in the same way.
See the doc.
if not, then not wss, but ws.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question