Search code examples
javascriptpythonsocket.ioeventletflask-socketio

Python SocketIO emitting event after server creation


I have a web project using php and a python script which modifies folders. I want to execute some javascript on the website based on eg. a folder creation done in the python script.

My idea was to work with python socketio. I have the basic connection notification working and emits from the js on a website are being handle by the js as well.

The problem is that I cannot emit any events after starting the eventlet server. I tried setting up the async mode but it does not have the desired result.

Here is my code so far:

import socketio
from flask import Flask, render_template
import eventlet

import sys

sio = socketio.Server(async_mode='eventlet')
app = Flask(__name__)

@sio.on('connect')
def connect(sid, environ):
    print('connect ', sid)
    sio.emit('channel', 'new connection')
    sys.stdout.flush()

@sio.on('mes')
def message(sid, data):
    print('message ', data)
    sys.stdout.flush()

app = socketio.Middleware(sio)

eventlet.wsgi.server(eventlet.listen(('', 8000)), app)

# create folder HERE and emit event which will be sent to browser

UPDATE:

I started using threads like in the following code: (Is there a problem with using threads like that? Or another better approach?)

import socketio
from flask import Flask, render_template
import eventlet

import sys
import thread
import time

sio = socketio.Server(async_mode='gevent')

@sio.on('connect')
def connect(sid, environ):
    print('connect ', sid)
    sio.emit('channel', 'new connection')
    sys.stdout.flush()

@sio.on('mes')
def message(sid, data):
   print('message ', data)
   sio.emit('channel', 'yeah')
   sys.stdout.flush()

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app = socketio.Middleware(sio)

def worker( threadName ):
   count = 1
   while count > 0:
      time.sleep(1)
      # trying to emit message HERE
      sio.emit('mes', 'foo ' + str(count))
      print "working " + str(count)
      sys.stdout.flush()
      count += 1

def server( threadName ):
   print "server starting"
   sys.stdout.flush()
   pywsgi.WSGIServer(('', 8000), app, handler_class=WebSocketHandler).serve_forever()

try:
   thread.start_new_thread( worker, ("Thread-1", ) )
   thread.start_new_thread( server, ("Thread-2", ) )
except:
   print "Error: unable to start thread"

while 1:
   pass

The problem now is that the sio.emit('mes', 'foo ' + str(count)) emits the message - but the browser only catches it every 20~30s and then all missing events are handled. See the picture with the frames from the dev console.

network frames


Solution

  • With the comments by @Miguel I accomplished to reach to goal I was aiming for.

    I removed the thread for the server which seemed to have the desired effect. Now the developer console shows a emitted event every time the worker fires an event.

    import socketio
    import sys
    import thread
    import time
    
    sio = socketio.Server(async_mode='gevent')
    
    @sio.on('connect')
    def connect(sid, environ):
        print('connect ', sid)
        sio.emit('channel', 'new connection')
        sys.stdout.flush()
    
    @sio.on('mes')
    def message(sid, data):
       print('message ', data)
       sio.emit('channel', 'yeah')
       sys.stdout.flush()
    
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    app = socketio.Middleware(sio)
    
    def worker( threadName ):
       count = 1
       while count > 0:
          time.sleep(5)
          sio.emit('mes', 'foo ' + str(count))
          print "working " + str(count)
          sys.stdout.flush()
          count += 1
    
    try:
       thread.start_new_thread( worker, ("Thread-1", ) )
    except:
       print "Error: unable to start thread"
    
    print "server starting"
    sys.stdout.flush()
    pywsgi.WSGIServer(('', 8000), app, handler_class=WebSocketHandler).serve_forever()