Search code examples
pythonflasksocket.ioflask-socketio

Flask SocketIO emitting on multiple events


EDIT: The first method works fine, I was emitting the wrong thing

I am looking for a way to emit the same thing on multiple events, on top of emitting event specific information.

I have tried something like this which doesn't work (I assume this doesn't work because I cannot emit from a method outside of socketIO context):

@socketio.on("event1")
def handle_event_1(data):
    emit(...)
    do_common_operation(data)

@socketio.on("event2")
def handle_event_2(data):
    emit(...)
    do_common_operation(data)

def do_common_operation(data):
    emit(...)

I have also tried something like this which doesn't work (This only executes either the event1/event2 or the common operation, but not both):

@socketio.on("event1")
def handle_event_1(data):
    emit(...)

@socketio.on("event2")
def handle_event_2(data):
    emit(...)

@socketio.on("event1")
@socketio.on("event2")
def do_common_operation(data):
    emit(...)

I could probably copy the common operation code into both event handlers, but that seems like a somewhat dumb solution, because there is more code than in this small example.

Any suggestions to handle this in a clean way?


Solution

  • The first method appears to work fine, I had a bug in my code