Skip to content

Implementation in Django #712

@Marco-Gtto

Description

@Marco-Gtto

Hi @miguelgrinberg,
I am developing a project with Django, in which I have recently started integrating socket.io.
I followed the documentation and your specific examples about it, getting to this point:

views.py

from django.shortcuts import render
import socketio
from .models import User

sio = socketio.Server(async_mode='eventlet', logger=True)

@sio.event
def connect(sid, environ):
    print('connect ', sid)


@sio.event
def disconnect(sid):
    print('disconnect ', sid)


@sio.on('user_update')
def user_update(sid, data):
    pass


@sio.on('user_sanction')
def user_sanction(sid, data):
    pass

wsgi.py

import os
import eventlet
import socketio
import eventlet
import eventlet.wsgi
from django.core.wsgi import get_wsgi_application

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

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

eventlet.wsgi.server(eventlet.listen(('', 5000)), application)

In my project I created an app that will only take care of updating the database in relation to the data received via socket. I have currently started writing code for this in views.py, although I would like to create a dedicated file for this purpose. Essentially the problem is that in the console I read the following strings:

(8180) wsgi starting up on http://0.0.0.0:5000
(8180) accepted ('127.0.0.1', 50224)

however the event handlers in views.py seem not to exist, as after connection I don't receive any message in the console.
I'm not actually able to figure out if I need to invoke something somewhere or add more in wsgi to get the event handlers in question to work.
I hope for some advice that can help me continue.

Thanks in advance
Marco

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions