title: How to wait for a callback passed to Flask-SocketIO's emit()?
body: <p>Is there a way in <code>Flask-SocketIO</code> to have a <em>blocking/synchronous</em> <code>emit('event', callback)</code> function that <strong>waits for the callback passed to it before returning</strong>?</p>
  
  <p><strong>Or</strong> -- is there a way to <strong><em>directly</em></strong> invoke the callback in an <code>@socketio.on('event')</code> handler instead of the plain <code>return</code> from that handler?</p>
  
  <hr>
  
  <h1>This is my situation specifically:</h1>
  
  <pre><code>+-----------------+                            +----------------------+                         +----------------------+
  |     Browser     |  emit('serverGiveData',    |        Flask         |  emit('workerGiveData', |       Worker         |
  |  (webapp, JS)   |       browser_callback)    |      web server      |       server_callback)  |   (Python program)   |
  |                 | +------------------------&gt; |                      | +--------------------&gt;  |                      |
  |                 |                            |                      |                         |                      |
  | socket.io 1.7.3 |            data            | Flask-SocketIO 2.8.2 |          data           |socketiIO-client 0.7.2|
  |                 | &lt;------------------------+ |                      | &lt;--------------------+  |                      |
  |                 |                            |                      |                         |                      |
  +-----------------+                            +----------------------+                         +----------------------+
  </code></pre>
  
  <p>So the <strong>Browser</strong> wants <strong>data</strong> from the <strong>Worker</strong> and the <strong>Flask web server</strong> is just a proxy in between.</p>
  
  <p>I would like the <code>browser_callback()</code> to be invoked after the <strong>Server</strong> receives the <strong>data</strong> from the <strong>Worker</strong>.<br>
  (I.e. I would like to call the <code>browser_callback()</code> from the <code>server_callback()</code>).</p>
  
  <p><strong><em>However</em></strong>, I cannot invoke the <code>browser_callback()</code> from <code>server_callback()</code> manually in Flask-SocketIO -- it is "automatically" invoked when I return from <code>'serverGiveData'</code> handler function on the <strong>Server</strong>. That is why I would like to have a <em>blocking/synchronous</em> <code>emit('workerGiveData')</code> so that the handler on the <strong>Server</strong> doesn't return before the <strong>Worker</strong> delivers the data.</p>
  
  <hr>
  
  <h1>Here's the code</h1>
  
  <h3>Browser</h3>
  
  <pre class="lang-js prettyprint-override"><code>socketio.emit('serverGiveData', args, function (data) {
      console.log('Received data');
  });
  </code></pre>
  
  <h3>Server</h3>
  
  <pre class="lang-py prettyprint-override"><code>@socketio.on('serverGiveData')
  def handler(msg):
      socketio.emit('workerGiveData', msg, callback=server_callback)
      return  # When server_callback() gets called back
  
  def server_callback(data):
      print('Received data from Worker')
      # Here I want to invoke client_callback(), i.e.
      # I don't want handler() to return before this server_callback() is invoked
  </code></pre>
  
  <h3>Worker</h3>
  
  <pre class="lang-py prettyprint-override"><code>def handler(args, callback);
      callback(data)
  
  socketIO.on('workerGiveData', handler)
  </code></pre>
  
  <hr>
  
  <p>(I am aware I could <code>emit('heyBrowserHeresData')</code> from the <code>server_callback()</code> when the Worker delivers the data and listen on that event in the browser with <code>browser_callback()</code> code as the handler.<br>
  I would like to avoid that jumble.)</p>