gevent-websocket / docs / index.rst


gevent-websocket is a WebSocket library for the gevent networking library written written and maintained by Jeffrey Gelens It is licensed under the BSD license.

from geventwebsocket import WebSocketServer, WebSocketApplication, Resource

class EchoApplication(WebSocketApplication):
    def on_message(self, message):

    ('', 8000),
    Resource({'/': EchoApplication})

Add WebSockets to your WSGI application

It isn't necessary to use the build-in WebSocketServer to start using WebSockets. WebSockers can be added to existing applications very easy by making the non-standard wsgi.websocket variable available in the WSGI environment. An example using Flask follows:

from geventwebsocket import WebSocketServer, WebSocketError
from flask import Flask, request, render_template

app = Flask(__name__)

def index():
    return render_template('index.html')

def api():
    ws = request.environ.get('wsgi.websocket')

    if not ws:
        abort(400, "Expected WebSocket request")

    while True:
            message = ws.receive()
            ws.send("Your message was: {}".format(message))
        except WebSocketError:
            # Possibility to execute code when connection is closed

if __name__ == '__main__':
    server = WebSocketServer(("", 8000), websocket_app)

Also the browser Javascript application can be very simple:

<!DOCTYPE html>
    var ws = new WebSocket("ws://localhost:8000/api");

    ws.onopen = function() {
        ws.send("Hello, world");
    ws.onmessage = function (event) {


  • Framework for WebSocket servers and WebSocket subprotocols
  • Implementation of RFC6455 and Hybi-10+
  • gevent based: high performance, asynchronous
  • standards conformance (100% passes the Autobahn Websocket Testsuite)


Distribute & Pip

Installing gevent-websocket is simple with pip:

$ pip install gevent-websocket

Get the Code

Requests is being developed on BitBucket.

You can clone the repsistory:

hg clone

or download the tarball:

curl -LO

Once you have a copy, you can either embed it in your application, or installed it on your system with:

$ python install


Main classes


Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.