Skip to content

GraftJS/jschan-ws

Repository files navigation

jschan websocket transport   Build Status

jschan is a JavaScript port of libchan based around node streams

Find out more about jschan

Install

npm install jschan-ws --save

ws.clientSession(url)

Creates a new websocket session. The url can be in the form 'ws://localhost' or passes as an object. It is based on streamSession.

ws.clientSession is not compatible with libchan.

This method can also work in the browser thanks to Browserify.

Example:

var websocket = require('jschan-ws');
var session = websocket.clientSession('ws://localhost:3000');
var chan    = session.WriteChannel();
var ret     = chan.ReadChannel();

ret.on('data', function(res) {
  console.log(res);
  session.close();
});

chan.end({
  hello:'world',
  returnChannel: ret
});

websocket.server(options)

Creates a new websocketServer, or attach the websocket handler to the passed-through httpServer object. It is based on streamSession.

websocket.server is not compatible with libchan.

If a new http server is created, remeber to call listen, like so:

'use strict';

var websocket = require('jschan-ws');
var server = websocket.server();

function handleMsg(msg) {
  var stream = msg.returnChannel;
  delete msg.returnChannel;
  stream.end(msg);
}

function handleChannel(chan) {
  chan.on('data', handleMsg);
}

function handleSession(session) {
  session.on('channel', handleChannel);
}

server.on('session', handleSession);

server.listen(3000);

About LibChan

It's most unique characteristic is that it replicates the semantics of go channels across network connections, while allowing for nested channels to be transferred in messages. This would let you to do things like attach a reference to a remote file on an HTTP response, that could be opened on the client side for reading or writing.

The protocol uses SPDY as it's default transport with MSGPACK as it's default serialization format. Both are able to be switched out, with http1+websockets and protobuf fallbacks planned. SPDY is encrypted over TLS by default.

While the RequestResponse pattern is the primary focus, Asynchronous Message Passing is still possible, due to the low level nature of the protocol.

Graft

The Graft project is formed to explore the possibilities of a web where servers and clients are able to communicate freely through a microservices architecture.

"instead of pretending everything is a local function even over the network (which turned out to be a bad idea), what if we did it the other way around? Pretend your components are communicating over a network even when they aren't." Solomon Hykes (of Docker fame) on LibChan - [link]

Find out more about Graft

Contributors

License

MIT