3

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.

SpeedOfRound
  • 1,210
  • 11
  • 26
  • 2
    The argument in the server should be `message_queue`, not `message_que`. For the job queue process, you do need a separate `socketio` instance that is initialized without the `app` argument. That is what configures the object to work as an external emitter. – Miguel Grinberg Jan 21 '23 at 14:18

1 Answers1

1

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

SpeedOfRound
  • 1,210
  • 11
  • 26