Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Cable is a fast and simple binary request/response protocol stream for Node.js
JavaScript
tag: v1.0.2

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
README.md
index.js
package.json
test.js

README.md

Cable

Cable is incredibly fast binary encoded JSON requests over a stream in Node.js with pipelining support. It's available through npm:

npm install cable

Usage

Cable is especially useful for fast communication between processes and servers. All JSON messages are sent with a 5 byte header that descripes the type and length of a message. This allows for extremely easy and fast parsing.

Usage is simple. Start a server:

var net = require('net');
var cable = require('cable');

var server = net.createServer(function(socket) {
    var c = cable();

    c.on('message', function(message, callback) {
        // lets just echo
        callback(null, {echo:message});
    });

    socket.pipe(c).pipe(socket); // setup the pipe chain
});

server.listen(8080);

Do a request:

var socket = net.connect(8080);
var c = cable();

socket.pipe(c).pipe(socket);

c.send({hello:'world'}, function(err, message) {
    console.log(message); // prints echo
});

c.send({hello:'verden'}, function(err, message) {
    console.log(message); // prints echo again
});

Pipelining

When you do a request c.send(message, [cb]) cable sends the message and pushes the callback to a response stack. If a new request is made to the same stream in the meantime cable will just send that right away and pipeline the request to minimize latency. Omit the callback to not wait for a response.

Full API

  • cable() -> c instantiate a new cable duplex stream

  • c.send(message, [cb]) send a message. add the callback if you want a response

  • c.on('message', message, cb) emitted when a message is received. cb is noop if no callback was used in send

  • c.destroy() destroy the stream. calls all missing callbacks with an error

  • c.ping(cb) send a ping.

License

MIT

Something went wrong with that request. Please try again.