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!
127.0.0.1looks to that local container, so either useredis(same as the service name), if within the same compose, ORhost.docker.internalif outwith that compose and you just want to access6379which is exposed on the host. - v25message_queue='redis://redis:6379'ORmessage_queue='redis://host.docker.internal:6379'- v25socketio = SocketIO()and thensocketio.init_app(app, message_queue='...'). - Miguelsocketio = SocketIO()socketio.init_app(app, message_queue='redis://127.0.0.1:6379')socketio.on_namespace(MyNamespace('/my_namespace'))- Tân Lê