Using it you can create real-time web-based application in any backend language you'd like.
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.
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 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.
- Sent when a client connects to the server. The session_id is set, while the data field is empty (but present in the message)
- Sent when a client disconnects from the server. Like connect, the session_id is set, but the data is empty.
- 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:
As of now the supported message_type are:
- Send this to forward a message to a client with session_id. data contains the message for the client.
- Disconnect the client with the given session_id. data is discarded but must be present in the message as a part (usually empty).
- 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 is done like any other Python package: pip install sockjsproxy.
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.
Once installed, you can launch the proxy in one terminal with
sockjsproxy --samples samples --verbose
and launch in another terminal
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).
- Added disconnectall capability to force all clients to disconnect.
- Switch from PUB/SUB socket pair to PUSH/PULL.
- Add support to force the client to disconnect.