reactivesocket-js
ReactiveSocket Protocol for Client/Server for JS. Also comes with a CLI.
npm install -g reactivesocket
This library only supports the request/response
, setup
and error
interactions. More interactions are coming soon.
Streams
The transport for this library is built entirely on top of the Node.js Stream API. As a result, it is agnostic to the underlying transport mechanism. As long as you pass in a transport stream that is a Node.js Duplex stream, this library will work.
Using streams means that this library natively supports backpressure regardless of the transport implementation.
We currently target TCP via the net module, and WebSockets via the yws-stream module. You are of course, free to inject other transports.
Connection Quick Start
This library supports 3 classes of clients. A fully managed load balancer, which automatically manages a pool of connections and takes care of automatically reconnecting and load-balancing connections from the pool. A fully managed single TCP connection, which can be configured to automatically reconnect if the TCP connection disconnects. Lastly a "raw" ReactiveSocket connection, which doesn't include any retry or transport logic. You provide a transport stream to plug in to the connection. This is the most flexible client, as you can use it with any transport mechanism. Examples for TCP and WebSockets are provided.
TCP Client Side Load Balancer
var bunyan = ;var reactiveSocket = ; var connectionPool = reactiveSocket; connectionPool;
TCP Connection
var bunyan = ;var reactiveSocket = ; var tcpConnection = reactiveSocket; tcpConnection;
Raw TCP
var net = ; var bunyan = ;var reactiveSocket = ; // Create any transport stream that's a Node.js Duplex Stream.var transportStream = net;
Raw WebSocket
var bunyan = ;var reactiveSocket = ; var Ws = ;var WSStream = ; var websocket = 'ws://localhost:1337'; // Create any transport stream that's a Node.js Duplex Streamvar transportStream = log: bunyan ws: websocket; // Wait for Websocket to establish connection, before we create an RS Connectionwebsocket;
Timeout
As a convenience for the user, the ReactiveSocket client provide 'timeout' events. Below is an example of how to listen to those events.
var socket = ...;var client = reactiveSocket; client;
Lease Semantic
ReactiveSocket client allows you to specify if you want to honor the lease semantic.
reactiveSocket;
If you don't, it means that the ReactiveSocket
is ready as
soon as the connection is established, and you can start sending messages.
But if you do, it means the client has to wait for a LEASE
frame from the
server before sending messages.
Note that nothing is preventing the client to send requests to the server before
receiving the LEASE
, the LEASE
reception only update the return value of
the availability
method (number between 0 and 1.0).
The availability
method gives precious information to a potential higher
level library (e.g. load-balancing library) about the capability of the
underlying connection.
More details about the lease semantic are available in the protocol Spec.
Metrics
ReactiveSocket uses the metrix library to collect and export metrics. By default, the library doesn't collect any metrics, and then its usage doesn't have any impact on the performance of ReactiveSocket.
To enable the collection, you need to pass a Recorder
when creating a ReactiveSocket, and
listen to this Recorder
with an Aggregator
.
E.g.
var RECORDER = metrix;var AGGREGATOR = metrix; var client = reactiveSocket; ;
CLI
This library comes with a CLI. You can use it by installing this module.
$ npm install -g reactivesocket
RS Client
There are two versions of the client CLI. The simple CLI makes one request to a server.
$ rs -o req tcp://localhost:1337 'if you didnt care what happened to me, And I didnt care for you'
There is also a benchmarking CLI in the vein of Apache Bench
$ rb -c 10 -n 10000000 -s 1000 tcp://localhost:1337
Echo Servers
Simple echo servers are also available for both TCP and Websocket.
TCP
$ HOST=localhost PORT=1337 rs-tcp-server
WebSocket
$ HOST=localhost PORT=1337 rs-ws-server
Contributions
Contributions welcome, please ensure make check
runs clean.
License
MIT
Copyright 2016 Yunong J Xiao