All Projects → flaviogrossi → sockjs-cyclone

flaviogrossi / sockjs-cyclone

Licence: MIT License
SockJS server support for the Cyclone web server

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to sockjs-cyclone

play2-sockjs
A SockJS server implementation for Play Framework.
Stars: ✭ 60 (+130.77%)
Mutual labels:  sockjs, sockjs-server
aioScrapy
基于asyncio与aiohttp的异步协程爬虫框架 欢迎Star
Stars: ✭ 34 (+30.77%)
Mutual labels:  twisted
Melkweg
Project Melkweg is only a tool testing the network latency between two computers.
Stars: ✭ 18 (-30.77%)
Mutual labels:  twisted
SnitchDNS
Database Driven DNS Server with a Web UI
Stars: ✭ 169 (+550%)
Mutual labels:  twisted
Cardinal
A Python IRC bot, designed to make adding functionality quick and simple.
Stars: ✭ 92 (+253.85%)
Mutual labels:  twisted
ncolony
A colony of interacting processes
Stars: ✭ 23 (-11.54%)
Mutual labels:  twisted
ROSE
ROSE project car race game
Stars: ✭ 24 (-7.69%)
Mutual labels:  twisted
tubes
A series of tubes.
Stars: ✭ 55 (+111.54%)
Mutual labels:  twisted
24-game
24 point game implemented in Python, just for fun!
Stars: ✭ 16 (-38.46%)
Mutual labels:  twisted
erk
Ərk is an open source, cross-platform IRC client written in Python 3, Qt 5, and Twisted.
Stars: ✭ 21 (-19.23%)
Mutual labels:  twisted
demo-spring-websocket
'WebSockets with Spring: HTTP and WebSocket; WebSocket with SockJS fallback; STOMP over WebSocket' articles and source code.
Stars: ✭ 30 (+15.38%)
Mutual labels:  sockjs
spring-websocket-angular6
Example for using Spring Websocket and Angular with Stomp Messaging
Stars: ✭ 18 (-30.77%)
Mutual labels:  sockjs
ng2-STOMP-Over-WebSocket
STOMP Over WebSocket service for angular2
Stars: ✭ 35 (+34.62%)
Mutual labels:  sockjs
tahoe-lafs-public-clouds
tahoe-lafs backend drivers for no-cost cloud providers
Stars: ✭ 25 (-3.85%)
Mutual labels:  twisted
kotori
A flexible data historian based on InfluxDB, Grafana, MQTT and more. Free, open, simple.
Stars: ✭ 73 (+180.77%)
Mutual labels:  twisted
prometheus-async
Async helpers for prometheus_client.
Stars: ✭ 136 (+423.08%)
Mutual labels:  twisted
STUP-Protocol
Secure/Speedup TCP-like UDP protocol
Stars: ✭ 12 (-53.85%)
Mutual labels:  twisted
girc
💬 A simple chat client in Python/Twisted
Stars: ✭ 16 (-38.46%)
Mutual labels:  twisted
spring-websocket-template
Template project for configuring websockets with spring
Stars: ✭ 32 (+23.08%)
Mutual labels:  sockjs
djangobot
Bridge between Slack and Django, via Channels
Stars: ✭ 66 (+153.85%)
Mutual labels:  twisted

SockJS-cyclone

https://badge.fury.io/py/sockjs-cyclone.png https://secure.travis-ci.org/flaviogrossi/sockjs-cyclone.png?branch=master https://pypip.in/d/sockjs-cyclone/badge.png

SockJS-cyclone is a pure Python server implementation for the SockJS client library running on the Cyclone web server.

SockJS-cyclone is released under the MIT license.

What is SockJS?

SockJS is a browser JavaScript library that provides a WebSocket-like object. SockJS gives you a coherent, cross-browser, JavaScript API which creates a low latency, full duplex, cross-domain communication channel between the browser and the web server, which consistently works across old browsers, misconfigured or old proxies and firewalls, etc. by automatically using other transports as a fallback mechanism.

SockJS main features:

  • simple APIs, as close to the WebSocket API as possible;
  • scaling and load balancing techniques;
  • very fast connection establishment;
  • pure JavaScript library on the client-side, no flash needed;
  • very extensive code testing available for both the server and client sides.

SockJS-cyclone fully supports the SockJS protocol version 0.3.3.

What is Cyclone?

Cyclone is a very fast and scalable web server framework that implements the Tornado API as a Twisted protocol.

How does it look like?

A live demo deployed on Heroku can be found here.

Here is a small example for an echo server:

from twisted.internet import reactor
import cyclone
import sockjs.cyclone

class EchoConnection(sockjs.cyclone.SockJSConnection):
    def messageReceived(self, message):
        self.sendMessage(message)

if __name__ == "__main__":
    EchoRouter = sockjs.cyclone.SockJSRouter(EchoConnection, '/echo')
    app = cyclone.web.Application(EchoRouter.urls)
    reactor.listenTCP(8888, app)
    reactor.run()

and an excerpt for the client:

var sock = new SockJS('http://mydomain.com/echo');
sock.onopen = function() {
    console.log('open');
};
sock.onmessage = function(e) {
    console.log('message', e.data);
};
sock.onclose = function() {
    console.log('close');
};
sock.send('hello!');

Complete examples can be found here.

Multiplexing

SockJS-Cyclone supports multiplexing (multiple distinct channels over a single shared connection):

from twisted.internet import reactor
import cyclone
from sockjs.cyclone.conn import SockJSConnection, MultiplexConnection
from sockjs.cyclone.router import SockJSRouter

class AnnConnection(SockJSConnection):
    def messageReceived(self, message):
        self.sendMessage('Ann received ' + message)

class BobConnection(SockJSConnection):
    def messageReceived(self, message):
        self.sendMessage('Bob received ' + message)

class CarlConnection(SockJSConnection):
    def messageReceived(self, message):
        self.sendMessage('Carl received ' + message)

if __name__ == "__main__":
    multiplexConnection = MultiplexConnection.create(ann=AnnConnection,
                                                     bob=BobConnection,
                                                     carl=CarlConnection)

    echoRouter = SockJSRouter(multiplexConnection, '/echo')

    app = cyclone.web.Application(echoRouter.urls)
    reactor.listenTCP(8888, app)
    reactor.run()

See the websocket-multiplex library for the client support, and the complete example.

Installation

Install from pypi with:

pip install sockjs-cyclone

or from the latest sources with:

git clone https://github.com/flaviogrossi/sockjs-cyclone.git
cd sockjs-cyclone
python setup.py install

SockJS-cyclone API

The main interaction with SockJS-cyclone happens via the two classes SockJSRouter and SockJSConnection.

SockJSConnection

The SockJSConnection class represent a connection with a client and contains the logic of your application. Its main methods are:

  • connectionMade(request): called when the connection with the client is established;
  • messageReceived(message): called when a new message is received from the client;
  • sendMessage(message): call when you want to send a new message to the client;
  • close(): close the connection;
  • connectionLost(): called when the connection with the client is lost or explicitly closed.

SockJSRouter

The SockJSRouter class routes the requests to the various connections according to the url prefix. Its main methods are:

  • __init__(connection, prefix, user_settings): bounds the given connection to the given url prefix;
  • urls: read only property to be used to initialize the cyclone application with all the needed sockjs urls.

Deployment

SockJS servers are usually deployed in production behind reverse proxies and/or load balancers. The most used options are currently Nginx and HAProxy.

For Heroku deployment, see the quickstart instructions here.

Nginx

Two major options are needed to fully support proxying requests to a SockJS-Cyclone server: setting the HTTP protocol version to 1.1 and passing upgrade headers to the server. The relevant portion of the required configuration is:

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass          http://<sockjs_server>:<port>;
        proxy_http_version  1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection "upgrade";
        proxy_set_header    Host $http_host;
        proxy_set_header    X-Real-IP $remote_addr;
    }

}

For websocket support, nginx version 1.3.13 or above is needed.

A working nginx.conf example can be found in the examples directory.

HAProxy

A complete example for HAProxy deployment and load balancing can be found on SockJS-Node Readme.

Credits

Thanks to:

  • Serge S. Koval for the tornado implementation;
  • VoiSmart s.r.l for sponsoring the project.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].