Nick
Nick

Reputation: 406

Client not receiving events from Flask-SocketIO server with Redis message queue

I want to add multiprocessing to my Flask-SocketIO server so I am trying to add a Redis message queue as per the Flask-SocketIO docs. Even without adding multiprocessing, the client is not receiving any events. Everything else is working fine (e.g. the web page is being served, HTTP requests are being made, database calls are being made). There are no error messages on the front or back end. Before I added the Redis queue it was working. I verified that the 'addname' SocketIO route is being hit and that the request.sid looks right. What am I doing wrong?

Very simplified server code:

external_sio = SocketIO(message_queue='redis://')

def requester(user, sid):

    global external_sio

    external_sio.emit('addname', {'data': 'hello'}, room=sid)
    # do some stuff with requests and databases
    external_sio.emit('addname', {'data': 'goodbye'}, room=sid)

def main():

    app = Flask(__name__,
                static_url_path='',
                static_folder='dist',
                template_folder='dist')

    socketio = SocketIO(app)

    @socketio.on('addname')
    def add_name(user):
        global external_sio
        external_sio.emit('addname', {'data': 'test'}, room=request.sid)
        requester(user.data, request.sid)

    socketio.run(app, host='0.0.0.0', port=8000)

if __name__ == '__main__':
    main()

Simplified client code (React Javascript):

const socket = SocketIOClient('ipaddress:8000')
socket.emit('addname', {data: 'somename'})
socket.on('addname', ({data}) => console.log(data))

Upvotes: 0

Views: 447

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67507

The main server also needs to be connected to the message queue. In your main server do this:

socketio = SocketIO(app, message_queue='redis://')

In your external process do this:

external_sio = SocketIO(message_queue='redis://')  # <--- no app on this one

Upvotes: 1

Related Questions