0
votes

I am trying to send and receive events from a client (running on an external process) to Flask-SocketIO server using redis as message_queue but the server doesn't receive any event the client emit.

The server:

from flask_socketio import SocketIO, Namespace
from flask import Flask


class MyNamespace(Namespace):
   def on_my_event(self, data):
        print('Data received:', data)

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO()
socketio.on_namespace(MyNamespace('/my_namespace'))
socketio.init_app(app, message_queue='redis://127.0.0.1:6379')

if __name__ == '__main__':
    app.run()

The client:

from flask_socketio import SocketIO
import time

socketio = SocketIO(message_queue='redis://127.0.0.1:6379')


def test():
    count = 0
    while True:
        print('Data:', count)
        socketio.emit('my_event', {'data': count},
                       namespace='/my_namespace',
                       broadcast=True)
        count += 1
        time.sleep(2)


if __name__ == '__main__':
    test()

Redis server is running in a docker container with docker-compose:

version: '3.3'

services:
    redis:
        build: ./redis
        volumes: 
          - ./redis/data:/data
        expose: 
          - 6379
        ports: 
          - 6379:6379

The redis-cli monitor output:

1576594467.587035 [0 172.22.0.1:38534] "PUBLISH" "flask-socketio" "\x80\x03}q\x00(X\x06\x00\x00\x00methodq\x01X\x04\x00\x00\x00emitq\x02X\x04\x00\x00\x00dataq\x03}q\x04h\x03K\x83sX\b\x00\x00\x00callbackq\x05NX\x04\x00\x00\x00roomq\x06NX\b\x00\x00\x00skip_sidq\aNX\x05\x00\x00\x00eventq\bX\b\x00\x00\x00my_eventq\tX\t\x00\x00\x00namespaceq\nX\r\x00\x00\x00/my_namespaceq\x0bX\a\x00\x00\x00host_idq\x0cX \x00\x00\x0053331c21d78649cea28d1ad9e1010179q\ru."
1576594469.590627 [0 172.22.0.1:38534] "PUBLISH" "flask-socketio" "\x80\x03}q\x00(X\x06\x00\x00\x00methodq\x01X\x04\x00\x00\x00emitq\x02X\x04\x00\x00\x00dataq\x03}q\x04h\x03K\x84sX\b\x00\x00\x00callbackq\x05NX\x04\x00\x00\x00roomq\x06NX\b\x00\x00\x00skip_sidq\aNX\x05\x00\x00\x00eventq\bX\b\x00\x00\x00my_eventq\tX\t\x00\x00\x00namespaceq\nX\r\x00\x00\x00/my_namespaceq\x0bX\a\x00\x00\x00host_idq\x0cX \x00\x00\x0053331c21d78649cea28d1ad9e1010179q\ru."
1576594471.593944 [0 172.22.0.1:38534] "PUBLISH" "flask-socketio" "\x80\x03}q\x00(X\x06\x00\x00\x00methodq\x01X\x04\x00\x00\x00emitq\x02X\x04\x00\x00\x00dataq\x03}q\x04h\x03K\x85sX\b\x00\x00\x00callbackq\x05NX\x04\x00\x00\x00roomq\x06NX\b\x00\x00\x00skip_sidq\aNX\x05\x00\x00\x00eventq\bX\b\x00\x00\x00my_eventq\tX\t\x00\x00\x00namespaceq\nX\r\x00\x00\x00/my_namespaceq\x0bX\a\x00\x00\x00host_idq\x0cX \x00\x00\x0053331c21d78649cea28d1ad9e1010179q\ru."

The server does not print out any thing, please help me!

1
Where are the client and server code running in relation to the redis docker service? My suspicion: If they are in a docker container 127.0.0.1 looks to that local container, so either use redis (same as the service name), if within the same compose, OR host.docker.internal if outwith that compose and you just want to access 6379 which is exposed on the host. - v25
As in: message_queue='redis://redis:6379' OR message_queue='redis://host.docker.internal:6379' - v25
The server and client are running in localhost not in a container. I think the redis server is accessible for both client and server via 127.0.0.1:6379 because the redis-cli monitor command show that the redis is getting event from the client. - Tân Lê
You are mixing the direct and delayed initialization methods for the SocketIO extension in your server. Use socketio = SocketIO() and then socketio.init_app(app, message_queue='...'). - Miguel
@Miguel: I change my server to this but it still doesn't work: socketio = SocketIO() socketio.init_app(app, message_queue='redis://127.0.0.1:6379') socketio.on_namespace(MyNamespace('/my_namespace')) - Tân Lê

1 Answers

0
votes

I think you are confusing regarding what's a Socket.IO client. The code that you defined under the "The client" heading is not a client, it is an external process that runs on the server and can emit on its behalf.

As far as I can see everything works well. This external process is emitting to the client through the server. You are expecting the my_event handler on your server will receive this, but this event is going the other way, a client will receive it. Based on your description of the issue it appears you do not have any clients connected, so the message doesn't go anywhere.

If you want to have a proper client written in Python, then drop the message queue which isn't needed, and instead code a client using the Client or AsyncClient classes in the python-socketio package.