| 1 |
================ |
| 2 |
gevent-websocket |
| 3 |
================ |
| 4 |
|
| 5 |
`gevent-websocket`_ is a WebSocket library for the gevent_ networking library. |
| 6 |
|
| 7 |
Features include: |
| 8 |
|
| 9 |
- Integration on both socket level or using an abstract interface. |
| 10 |
- RPC and PubSub framework using `WAMP`_ (WebSocket Application |
| 11 |
Messaging Protocol). |
| 12 |
- Easily extendible using a simple WebSocket protocol plugin API |
| 13 |
|
| 14 |
|
| 15 |
:: |
| 16 |
|
| 17 |
from geventwebsocket import WebSocketServer, WebSocketApplication, Resource |
| 18 |
|
| 19 |
class EchoApplication(WebSocketApplication): |
| 20 |
def on_open(self): |
| 21 |
print "Connection opened" |
| 22 |
|
| 23 |
def on_message(self, message): |
| 24 |
self.ws.send(message) |
| 25 |
|
| 26 |
def on_close(self, reason): |
| 27 |
print reason |
| 28 |
|
| 29 |
WebSocketServer( |
| 30 |
('', 8000), |
| 31 |
Resource({'/': EchoApplication}) |
| 32 |
).serve_forever() |
| 33 |
|
| 34 |
or a low level implementation:: |
| 35 |
|
| 36 |
from gevent import pywsgi |
| 37 |
from geventwebsocket.handler import WebSocketHandler |
| 38 |
|
| 39 |
def websocket_app(environ, start_response): |
| 40 |
if environ["PATH_INFO"] == '/echo': |
| 41 |
ws = environ["wsgi.websocket"] |
| 42 |
message = ws.receive() |
| 43 |
ws.send(message) |
| 44 |
|
| 45 |
server = pywsgi.WSGIServer(("", 8000), websocket_app, |
| 46 |
handler_class=WebSocketHandler) |
| 47 |
server.serve_forever() |
| 48 |
|
| 49 |
More examples can be found in the ``examples`` directory. Hopefully more |
| 50 |
documentation will be available soon. |
| 51 |
|
| 52 |
Installation |
| 53 |
------------ |
| 54 |
|
| 55 |
The easiest way to install gevent-websocket is directly from PyPi_ using pip or |
| 56 |
setuptools by running the commands below:: |
| 57 |
|
| 58 |
$ pip install gevent-websocket |
| 59 |
|
| 60 |
|
| 61 |
Gunicorn Worker |
| 62 |
^^^^^^^^^^^^^^^ |
| 63 |
|
| 64 |
Using Gunicorn it is even more easy to start a server. Only the |
| 65 |
`websocket_app` from the previous example is required to start the server. |
| 66 |
Start Gunicorn using the following command and worker class to enable WebSocket |
| 67 |
funtionality for the application. |
| 68 |
|
| 69 |
:: |
| 70 |
|
| 71 |
gunicorn -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" wsgi:websocket_app |
| 72 |
|
| 73 |
Performance |
| 74 |
^^^^^^^^^^^ |
| 75 |
|
| 76 |
`gevent-websocket`_ is pretty fast, but can be accelerated further by |
| 77 |
installing `wsaccel <https://github.com/methane/wsaccel>`_ and `ujson` or `simplejson`:: |
| 78 |
|
| 79 |
$ pip install wsaccel ujson |
| 80 |
|
| 81 |
`gevent-websocket`_ automatically detects ``wsaccell`` and uses the Cython |
| 82 |
implementation for UTF8 validation and later also frame masking and |
| 83 |
demasking. |
| 84 |
|
| 85 |
Get in touch |
| 86 |
^^^^^^^^^^^^ |
| 87 |
|
| 88 |
Get in touch on IRC #gevent on Freenode or on the Gevent `mailinglist |
| 89 |
<https://groups.google.com/forum/#!forum/gevent>`_. Issues can be created |
| 90 |
on `Bitbucket <https://bitbucket.org/Jeffrey/gevent-websocket/issues?status=new&status=open>`_. |
| 91 |
|
| 92 |
.. _WAMP: http://www.wamp.ws |
| 93 |
.. _gevent-websocket: http://www.bitbucket.org/Jeffrey/gevent-websocket/ |
| 94 |
.. _gevent: http://www.gevent.org/ |
| 95 |
.. _Jeffrey Gelens: http://www.gelens.org/ |
| 96 |
.. _PyPi: http://pypi.python.org/pypi/gevent-websocket/ |
| 97 |
.. _repository: http://www.bitbucket.org/Jeffrey/gevent-websocket/ |
| 98 |
.. _RFC6455: http://datatracker.ietf.org/doc/rfc6455/?include_text=1 |