Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Socketio emitting from an external process

I've been working on a project using flask, flask-socketio and redis

I have a server, and some modules I would like to be able to emit from outside of the server file.

server.py

from flask import Flask, Response, request, json
from flask_socketio import SocketIO, join_room, leave_room, emit

app = Flask(__name__)

socketio = SocketIO()

socketio.init_app(
    app, 
    cors_allowed_origins="*",
    message_que="redis://127.0.0.1:6379/0"
)

@socketio.on('ready')
def ready(data):
    socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')
...

jobque.py

from modules.server.server import socketio

...

socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')

As it's currently configured, emits from the server file all work, the clients respond and they can talk back and forth. But when jobque goes to emit the same message, nothing happens. There's no error, and the client doesn't hear it.

I'm also using redis for things other than the websockets, I can get and set from it with no problem, in both files.

What do I need to do to get this external process to emit? I've looked through the flask-socketio documentation and this is exactly how they have it setup.

I've also tries creating a new SocketIO object inside jobque.py instead of importing the one form the server, but the results are the same

socket = SocketIO(message_queue="redis://127.0.0.1:6379/0")
socketio.emit('rollCall', { 'message': 'Ive got work for you' }, room='Ready')

I also went and checked if I could see the websocket traffic in redis with the message que setup using redis-cli > MONITOR, but I don't see any. I only see the operations I'm using redis for directly with the redis module. This makes me think the message que isn't actually being used, but I can't know for sure.

like image 958
SpeedOfRound Avatar asked Dec 05 '25 20:12

SpeedOfRound


1 Answers

Unfortunately spelled message_queue as message_que was the issue. Creating a new SocketIO instance without the app works now.

like image 92
SpeedOfRound Avatar answered Dec 08 '25 08:12

SpeedOfRound



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!