gevent-websocket icon indicating copy to clipboard operation
gevent-websocket copied to clipboard

Official mirror of the repository at Gitlab.

================ gevent-websocket

gevent-websocket_ is a WebSocket library for the gevent_ networking library.

Features include:

  • Integration on both socket level or using an abstract interface.
  • RPC and PubSub framework using WAMP_ (WebSocket Application Messaging Protocol).
  • Easily extendible using a simple WebSocket protocol plugin API

::

from geventwebsocket import WebSocketServer, WebSocketApplication, Resource
from collections import OrderedDict

class EchoApplication(WebSocketApplication):
    def on_open(self):
        print("Connection opened")

    def on_message(self, message):
        self.ws.send(message)

    def on_close(self, reason):
        print(reason)

WebSocketServer(
    ('', 8000),
    Resource(OrderedDict([('/', EchoApplication)]))
).serve_forever()

or a low level implementation::

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler

def websocket_app(environ, start_response):
    if environ["PATH_INFO"] == '/echo':
        ws = environ["wsgi.websocket"]
        message = ws.receive()
        ws.send(message)

server = pywsgi.WSGIServer(("", 8000), websocket_app,
    handler_class=WebSocketHandler)
server.serve_forever()

More examples can be found in the examples directory. Hopefully more documentation will be available soon.

Installation

The easiest way to install gevent-websocket is directly from PyPi_ using pip or setuptools by running the commands below::

$ pip install gevent-websocket

Gunicorn Worker ^^^^^^^^^^^^^^^

Using Gunicorn it is even more easy to start a server. Only the websocket_app from the previous example is required to start the server. Start Gunicorn using the following command and worker class to enable WebSocket funtionality for the application.

::

gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" wsgi:websocket_app

Performance ^^^^^^^^^^^

gevent-websocket_ is pretty fast, but can be accelerated further by installing wsaccel <https://github.com/methane/wsaccel>_ and ujson or simplejson::

$ pip install wsaccel ujson

gevent-websocket_ automatically detects wsaccell and uses the Cython implementation for UTF8 validation and later also frame masking and demasking.

Get in touch ^^^^^^^^^^^^

Get in touch on IRC #gevent on Freenode or on the Gevent mailinglist <https://groups.google.com/forum/#!forum/gevent>. Issues can be created on Bitbucket <https://bitbucket.org/Jeffrey/gevent-websocket/issues?status=new&status=open>.

.. _WAMP: http://www.wamp.ws .. _gevent-websocket: http://www.bitbucket.org/Jeffrey/gevent-websocket/ .. _gevent: http://www.gevent.org/ .. _Jeffrey Gelens: http://www.gelens.org/ .. _PyPi: http://pypi.python.org/pypi/gevent-websocket/ .. _repository: http://www.bitbucket.org/Jeffrey/gevent-websocket/ .. _RFC6455: http://datatracker.ietf.org/doc/rfc6455/?include_text=1