Skip to content

Trouble with handlers in flask-socketio that seem to work in python-socketio #1508

Description

@DerJungeDev

I was working with flask_socketio and I have been running into numerous obstacles. My working directory looks a little like

run.py
myapp
  __init__.py
  routes.py
static
  styles
    count.css
  scripts
    count.js

My very simple run .py is

from myapp import app, socketio
from flask_socketio import SocketIO

if __name__ == '__main__':
    port = 9999
    socketio.run(app,port=port,log_output=True, debug=True)

My __init__.py looks like

from myapp import app, socketio
from flask_socketio import SocketIO

if __name__ == '__main__':
    port = 9999
    socketio.run(app,port=port,log_output=True, debug=True)

routes.py looks like

from flask import render_template, url_for, flash, redirect
from myapp import app, socketio

from flask_socketio import send, emit


@app.route('/')
def index():
    return render_template('count.html')

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

@socketio.event
def disconnected(sid):
    print(sid, 'disconnected')

and the count.js which is linked through the count.html file

//client for socketio
const sio = io();

console.log('test')

sio.on('connect', ()=>{
  console.log('connected');
});

sio.on('disconnect', ()=>{
  console.log('disconnected');
});

I keep getting errors saying that I implemented the connect event handler wrong, I don't have the arguments sid and environ (which I'm pretty sure I do), and other little issues

TypeError: connect() missing 2 required positional arguments: 'sid' and 'environ'

After watching Miguel's tutorials of python socketio on youtube, and reading through flask-sockectio documentation, I was able to implement the connect handler with (sid, environ) arguments but does not seem to work in flask-socketio

If anyone could help me or lead me to a tutorial or something where someone makes something with flask and socketio (preferably with a split up flask app like mine) I would highly appreciate it

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions