question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Django 3 and Daphne deploy

See original GitHub issue

Hi,

I’m trying to implement a simple socket emit with Django 3 and Daphne, but for some reason the event emited isn’t never captured by the client. Here is my code

asgi.py

import os
import socketio

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'eleicoes.settings')

sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*')
application = socketio.ASGIApp(sio, get_asgi_application())

views.py

from eleicoes.asgi import sio

# other code

print('emit vote event', sio)
sio.emit('user', UserSerializer(instance=request.user).data)

# other code

It simples doesn’t work. I think I followed the documentation in this matter but I can’t find anything with this specific setup (django 3 and daphne)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
miguelgrinbergcommented, Jan 16, 2020

I suggest you remove cors_allowed_origins='*'. This is basically allowing anybody to connect to your server, and can open the door to CSRF attacks. My recommendation is that you explicitly name the allowed origins instead.

1reaction
nunesvictorcommented, Jan 16, 2020

I honestly don’t know how to thank you. Apparently the problem was I had added the wrong async_mode on socketio.Server. I’ll put the correct configuration down here to whoever this may help in the future.

myapp.wsgi

import os
import socketio

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

sio = socketio.Server(
    async_mode='gevent_uwsgi',
    # implement allowed origins
    # cors_allowed_origins='*',
    # uncomment to enable logging
    # engineio_logger=True,
    # logger=True,
)

application = socketio.WSGIApp(sio, get_wsgi_application())

uWSGI command line uwsgi --http :8000 --gevent 1000 --http-websockets --master --module myapp.wsgi:application

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Django with Daphne
When Daphne is installed, a daphne command is available which starts the Daphne server process. At its simplest, Daphne needs to be called...
Read more >
Deploying — Channels 4.0.0 documentation
Channels (ASGI) applications deploy similarly to WSGI applications - you load them into a server, like Daphne, and you can scale the number...
Read more >
Configuring ASGI Django Application using Daphne and ...
My WSGI Django Application was already deployed using Gunicorn server and Nginx was used to serve static files and provide proxy to gunicorn ......
Read more >
Django deployment with Daphne - nginx - Stack Overflow
I want to deploy my Django app (Rest API + React on frontend) on AWS. I use nginx, gunicorn (for http handling) and...
Read more >
Daphne - Django Channels HTTP/WebSocket server - GitHub
Daphne is a HTTP, HTTP2 and WebSocket protocol server for ASGI and ASGI-HTTP, developed to power Django Channels. It supports automatic negotiation of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found