Server integration layers

As gevent-socketio runs on top of Gevent, you need a Gevent-based server, to yield the control cooperatively to the Greenlets in there.


If you have a python file that includes a WSGI application, for gunicorn integration all you have to do is include the socketio.sgunicorn

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app

paster / Pyramid’s pserve

Through Gunicorn

Gunicorn will handle workers for you and has other features.

For paster, you just have to define the configuration like this:

use = egg:gunicorn#main
host =
port = 6543
workers = 4
worker_class = socketio.sgunicorn.GeventSocketIOWorker

Directly through gevent

Straight gevent integration is the simplest and has no dependencies.

In your .ini file:

use = egg:gevent-socketio#paster
host =
port = 6543
resource =
transports = websocket, xhr-polling, xhr-multipart
policy_server = True
policy_listener_host =
policy_listener_port = 10843

policy_listener_host defaults to host, policy_listener_port defaults to 10843, transports defaults to all transports, policy_server defaults to False in here, resource defaults to

So you can have a slimmed-down version:

use = egg:gevent-socketio#paster
host =
port = 6543

django runserver

You can either define a wsgi app and launch it with gunicorn:

import django.core.handlers.wsgi
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
app = django.core.handlers.wsgi.WSGIHandler()

from commandline:

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker wsgi:app

or you can use gevent directly:

#!/usr/bin/env python
from gevent import monkey
from socketio.server import SocketIOServer
import django.core.handlers.wsgi
import os
import sys


    import settings
except ImportError:
    sys.stderr.write("Error: Can't find the file '' in the directory containing %r. It appears you've customized things.\nYou'll have to run, passing it your settings module.\n(If the file does indeed exist, it's causing an ImportError somehow.)\n" % __file__)

PORT = 9000

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

application = django.core.handlers.wsgi.WSGIHandler()

sys.path.insert(0, os.path.join(settings.PROJECT_ROOT, "apps"))

if __name__ == '__main__':
    print 'Listening on and on port 10843 (flash policy server)' % PORT
    SocketIOServer(('', PORT), application, resource="").serve_forever()


Since gevent is a cooperative concurrency library, no process or routine or library must block on I/O without yielding control to the gevent hub, if you want your application to be fast and efficient. Making these libraries compatible with such a concurrency model is often called greening, in reference to Green threads.

You will need `green`_ databases APIs to gevent to work correctly. See:

Web server front-ends

If your web server does not support websockets, you will not be able to use this transport, although the other transports may work. However, this would diminish the value of using real-time communications.

The websocket implementation in the different web servers is getting better every day, but before investing too much too quickly, you might want to have a look at your web server’s status on the subject.


nginx status

Nginx added the ability to support websockets with version 1.3.13 but it requires a bit of explicit configuration.


Assuming your config is setup to proxy to your gevent server via something like this:

location / {
    proxy_redirect off;

You’ll just need to add this additional location section. Note in this example we’re using / as the entry point (you might have to change it)

location / {
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

Make sure you’re running the latest version of Nginx (or atleast >= 1.3.13). Older versions don’t support websockets, and the client will have to fallback to long polling.


Using HAProxy to load-balance