GEVENT-WEBSOCKET(1)

πŸ“¦ version 0.10.1, in arch rolling

🌸 NAME 🌸

okay bestie, this is the GEVENT-WEBSOCKET(1) page, aka gevent-websocket - gevent-websocket Documentation. gevent-websocket is a WebSocket library for the gevent networking library. The WebSocket reference is at http://www.websocket.org/aboutwebsocket.html, and gevent is at http://www.gevent.org. It’s written written and maintained by Jeffrey Gelens (http://www.noppo.pro) β€” yes, the double β€œwritten” is literally on the page πŸ’€ β€” and it is licensed under the BSD license.

Here’s the starter echo application vibe:

from geventwebsocket import WebSocketServer, WebSocketApplication, Resource

class EchoApplication(WebSocketApplication):
    def on_message(self, message):
        self.ws.send(message)

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

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 (http://flask.pocoo.org) follows:

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

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/api')
def api():
    ws = request.environ.get('wsgi.websocket')

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

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

if __name__ == '__main__':
    server = WebSocketServer(("", 8000), app)
    server.serve_forever()

And the browser JavaScript application can be very simple too:

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

    ws.onopen = function() {
        ws.send("Hello, world");
    };
    ws.onmessage = function (event) {
        alert(event.data);
    };
  </script>
</head>
</html>

The features are giving main character energy ✨:


πŸ’– DISTRIBUTE & PIP πŸ’–

Installing gevent-websocket is simple with pip (http://www.pip-installer.org):

$ pip install gevent-websocket

πŸŽ€ GET THE CODE πŸŽ€

Requests is being developed on BitBucket.

You can clone the repsistory:

hg clone https://www.bitbucket.org/Jeffrey/gevent-websocket

or download the tarball:

curl -LO https://bitbucket.org/Jeffrey/gevent-websocket/TODO

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

$ python setup.py install

🧚 MAIN CLASSES 🧚


πŸ’€ EXCEPTIONS πŸ’€

  • Index <>
  • Module Index <>
  • Search Page <>

πŸ’œ Author πŸ’œ

Jeffrey Gelens


2013, Jeffrey Gelens


later bestie β€” this page is version 0.10, dated Jul 04, 2026 πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from