How to wait for a callback passed to Flask-SocketIO's emit()? How to wait for a callback passed to Flask-SocketIO's emit()? flask flask

How to wait for a callback passed to Flask-SocketIO's emit()?


The Socket.IO protocol is event-based, not request/response based. I recommend that you don't use the callbacks, those are for quick acknowledgement that an event was received, not to provide results after some work was done.

Try this instead to use a new event to replace your callback:

+-----------------+                            +----------------------+                         +----------------------+|     Browser     |  emit('serverGiveData’)    |        Flask         |  emit('workerGiveData', |       Worker         ||  (webapp, JS)   |                            |      web server      |       server_callback)  |   (Python program)   ||                 | +------------------------> |                      | +-------------------->  |                      ||                 |                            |                      |                         |                      || socket.io 1.7.3 |  emit(‘dataForBrowser’)    | Flask-SocketIO 2.8.2 |          data           |socketiIO-client 0.7.2||                 | <------------------------+ |                      | <--------------------+  |                      ||                 |                            |                      |                         |                      |+-----------------+                            +----------------------+                         +----------------------+

You can leave the second callback on the server-side if that works well for you, or less you can also replace it with an event.