title: How to emit message from python server to javascript client in python-socketio?
body: <p>The socketio client successfully connects to the server and sends messages with <code>emit</code> to the server but the other direction server to the client fails. I cannot find the source of error. It is </p>
  
  <p>Here is the server python in <code>app.py</code> based on the example in <a href="https://python-socketio.readthedocs.io/" rel="nofollow noreferrer">python-socketio</a> website:</p>
  
  <pre><code>from aiohttp import web
  import socketio
  
  sio = socketio.AsyncServer()
  app = web.Application()
  sio.attach(app)
  
  async def index(request):
      """Serve the client-side application."""
      with open('index.html') as f:
          return web.Response(text=f.read(), content_type='text/html')
  
  @sio.on('connect', namespace='/chat')
  def connect(sid, environ):
      print("connect", sid)
  
  @sio.on('chat message', namespace='/chat')
  async def message(sid, data):
      print("server received message!", data)
      await sio.emit('reply', data)
      await sio.send(data)
  
  @sio.on('disconnect', namespace='/chat')
  def disconnect(sid):
      print('disconnect', sid)
  
  app.router.add_static('/static', 'static')
  app.router.add_get('/', index)
  
  if __name__ == '__main__':
      web.run_app(app)
  </code></pre>
  
  <p>I tried commenting one of <code>await sio.emit('reply', data)</code> or <code>await sio.send(data)</code> but result was the same. Here is the javascript client in <code>index.html</code>:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form id="the_form"&gt;
        &lt;input type="input" name="msg" id="msg"&gt;&lt;/input&gt;
        &lt;input type="submit" value="➤"&gt;&lt;/input&gt;
      &lt;/form&gt;
      &lt;script&gt;
        var socket = io('http://localhost:8080/chat');
  
        socket.on('connect', function(){console.log('connect!')});
        socket.on('message', function(msg){console.log('message!', msg)});
        socket.on('disconnect', function(){console.log('disconnect!')});
        socket.on('reply', function(msg){console.log('reply!', msg)});
  
        document.getElementById('the_form').onsubmit = function(e) {
          let msg = document.getElementById('msg').value;
          document.getElementById('msg').value = '';
  
          // send it to the server
          socket.emit('chat message', msg);
  
          return false
        };
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>On the terminal window, I run the server. Then, I open two browser windows (Chrome Version 69.0.3497.100 (Official Build) (64-bit)) and send 'test' from one of them. Here is what I see on each window:</p>
  
  <p><strong>Terminal</strong></p>
  
  <pre><code>$ python3 app.py 
  ======== Running on http://0.0.0.0:8080 ========
  (Press CTRL+C to quit)
  connect 9b18034f7b8b4d4c857dec394ef01429
  connect 3adea48a3e00459f807855da0337599c
  server received message! test
  </code></pre>
  
  <p><strong>Window 1 (console log)</strong></p>
  
  <pre><code>connect!
  </code></pre>
  
  <p><strong>Window 2 (console log)</strong></p>
  
  <pre><code>connect!
  </code></pre>