1

I am trying to use daphne in django project to handle the websocket connections. I have installed daphne and it seems to be running. However, I am unable to send any websocket connection request. This is my daphne.service file:

[Unit]
Description=WebSocket Daphne Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/home/django/AysleServer/src
ExecStart=/home/django/AysleServer/MyEnv/bin/python /home/django/AysleServer/MyEnv/bin/daphne -e ssl:8001:privateKey=/etc/letsencrypt/live/djangoadmin.aysle.tech/privk>
Restart=on-failure

[Install]
WantedBy=multi-user.target

On checking the logs of Daphne it shows this:

Started WebSocket Daphne Service.
Starting server at ssl:8001:privateKey=/etc/letsencrypt/live/djangoadmin.aysle.tech/privk...>
HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Configuring endpoint ssl:8001:privateKey=/etc/letsencrypt/live/djangoadmin.aysle.tech/pri...>
Listening on TCP address 0.0.0.0:8001

I thought that http2 and tls were causing the issue so tried to install them in my virtual environment using the command:

pip install -U 'Twisted[tls,http2]'

But they were already present as shown below:

Requirement already satisfied: Twisted[http2,tls] in /usr/lib/python3/dist-packages (18.9.0)
Requirement already satisfied: idna!=2.3,>=0.6 in /usr/lib/python3/dist-packages (from Twisted[http2,tls]) (2.8)
Requirement already satisfied: pyopenssl>=16.0.0 in /usr/lib/python3/dist-packages (from Twisted[http2,tls]) (19.0.0)
Requirement already satisfied: service_identity in /usr/lib/python3/dist-packages (from Twisted[http2,tls]) (18.1.0)
Requirement already satisfied: h2<4.0,>=3.0 in /usr/local/lib/python3.8/dist-packages (from Twisted[http2,tls]) (3.2.0)
Requirement already satisfied: priority<2.0,>=1.1.0 in /usr/local/lib/python3.8/dist-packages (from Twisted[http2,tls]) (1.3.0)
Requirement already satisfied: hpack<4,>=3.0 in /usr/local/lib/python3.8/dist-packages (from h2<4.0,>=3.0->Twisted[http2,tls]) (3.0.0)
Requirement already satisfied: hyperframe<6,>=5.2.0 in /usr/local/lib/python3.8/dist-packages (from h2<4.0,>=3.0->Twisted[http2,tls]) (5.2.0)

I am using Gunicorn to handle http requests and it works as expected. This is my asgi.py file

import os
import django
from channels.routing import get_default_application
from decouple import config

os.environ.setdefault("DJANGO_SETTINGS_MODULE", f'{config("PROJECT_NAME")}.settings')
django.setup()
application = get_default_application()

and this is my routing.py file:

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.urls import path
from wsocket.consumers import *

ws_patterns = [
    path('ws/order/', OrderConsumer.as_asgi()),
]

application = ProtocolTypeRouter({
        'websocket' : AllowedHostsOriginValidator(
            AuthMiddlewareStack(
                URLRouter(ws_patterns)
            )
        )
})

I tried sending the websocket request the following ways:

wss://djangoadmin.aysle.tech:8001/ws/order/
wss://djangoadmin.aysle.tech/ws/order/

But am not able to connect. Please assist me where am I going wrong. Thanks for your time in advance.

Shiny
  • 115
  • 1
  • 9
  • Have you got any solution? I am facing similar issue here https://stackoverflow.com/questions/73599840/failed-to-execute-send-on-websocket-still-in-connecting-state-while-using open bounty to fix it – Prabhakaran Sep 07 '22 at 02:35

1 Answers1

0

I think it might be useful to check the /var/log/syslog if anything was not blocked by the firewall and or any errors.

Is this locally or on the server?

ascendedcrow
  • 53
  • 1
  • 6
  • this is on the server – Shiny Jul 15 '21 at 08:21
  • @Shiny is the logs clean with no errors? Chrome has an extension on testing your socket connections: https://chrome.google.com/webstore/detail/websocket-test-client/fgponpodhbmadfljofbimhhlengambbn?hl=en – ascendedcrow Jul 16 '21 at 09:09