Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

SockJSProxy

SockJSProxy is a simple proxy server that proxies message from SockJS to a ZeroMQ socket and vise-versa.

Using it you can create real-time web-based application in any backend language you'd like.

Motivation

Implementing the SockJS protocol is not trivial, as it supports a handful of transports. However, good ZeroMQ bindings exist for pretty much every popular language. This simple proxy allows the use of Real-time web communication (via SockJS) for nearly every popular language.

Implementation

This proxy is implemeted in Python using the sockjs-tornado and pyzmq librries. It uses the Tornado Web Server, which is a non-blocking server and should easily handle 1000s of clients.

ZeroMQ protocol

SockJSProxy uses a simple ZeroMQ multipart-based message protocol to proxy messages. You should implement the other end of this protocol, but that should be quite easy.

It binds to two sockets.


The incomming messages (i.e. sent by the client, usually a browser) socket is a PUSH one (by default on port 9241), where the proxy will push messages to. All messages are 3-part messages in the form of:

message_type
session_id
data

message_type can be one of those, listed below. session_id can be used to identify the connection. A reconnect of the client will result in a new session id.

connect
Sent when a client connects to the server. The session_id is set, while the data field is empty (but present in the message)
disconnect
Sent when a client disconnects from the server. Like connect, the session_id is set, but the data is empty.
message
Sent when a client sends a message. The session_id is the sender's session and data contains the message (as bytes) from the client.

The outgoing (for messages you wish to send to the client) socket is a PULL one (by default on port 9242), where the proxy will read messages and forward them to the SockJS client (usually a browser).

The format is like the one for incomming messages:

message_type
session_id
data

As of now the supported message_type are:

message
Send this to forward a message to a client with session_id. data contains the message for the client.
disconnect
Disconnect the client with the given session_id. data is discarded but must be present in the message as a part (usually empty).
disconnectall
Disconnect all connected clients. session_id and data are discarded, but must be present in the message (leaving them empty is a good idea).

Installation

Installation is done like any other Python package: pip install sockjsproxy.

Usage

This proxy exposes a command sockjsproxy. Here is the usage for it.

usage: sockjsproxy [-h] [--version] [--address ADDRESS] [--in-port IN_PORT]
                   [--out-port OUT_PORT] [--http-port HTTP_PORT]
                   [--static-path STATIC_PATH] [--static-url STATIC_URL]
                   [--samples SAMPLES] [--verbose]

Proxy messages between sock.js and a 0MQ socket

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --address ADDRESS, -a ADDRESS
                        The 0MQ address to listen to. Defaults to * (meaning -
                        everything)
  --in-port IN_PORT, -i IN_PORT
                        The port to listen for incoming 0MQ connections.
                        Defaults to 9241
  --out-port OUT_PORT, -o OUT_PORT
                        The port to listen for outgoing 0MQ connections.
                        Defaults to 9242
  --http-port HTTP_PORT, -p HTTP_PORT
                        The port to listen for http (sock.js) connections.
                        Defaults to 8080
  --static-path STATIC_PATH, -s STATIC_PATH
                        The path to a local static directory to be served
                        under --static-url
  --static-url STATIC_URL, -u STATIC_URL
                        The url where the files in --static-path will be
                        served
  --samples SAMPLES     Serve samples under SAMPLES path
  --verbose, -v         Make the server output more information - useful for
                        debugging

Stopping the server

When the server is running in the foreground - pressing Ctrl+C would stop it. If you use something like nohup do deamonize it - send it SIGTERM (see man kill for more info) and it will gracefully stop.

Samples

The samples directory contains an example client and server that use the proxy.

Once installed, you can launch the proxy in one terminal with

sockjsproxy --samples samples --verbose

and launch in another terminal

datereply-sjp.py

Then visit http://localhost:8080/samples/, play with the demo, see the log output and play with the code. This simple datereply-sjp.py script is a simple echo server, that will reply with the current time to every message and force the client to disconnect (by sending a disconnect message to the proxy server).

Licence

MIT

Authors

  • Emil Ivanov

Changelog

0.7.1
  • Added disconnectall capability to force all clients to disconnect.
0.7
  • Switch from PUB/SUB socket pair to PUSH/PULL.
  • Add support to force the client to disconnect.

Recent activity

Emil Vladev

Commits by Emil Vladev were pushed to vladev/sockjsproxy

26b783b - Move to PUSH/PULL pairs. Support disconecting of clients
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 ProjectModifiedEvent.java.
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.