Channels as streams inside a stream, negotiated.
$ npm install pipe-channels --save
var Channels = require('pipe-channels');
var server = Channels.createServer();
// listen for new channel requests:
server.on('request', function(request) {
// request.payload has arbitrary values passed in by client
if (request.payload.token == 'this is a token') {
var channel = request.grant(); // channel is an object stream
} else {
request.deny('just because'); // deny the channel
}
});
// pipe server into duplex stream
stream.pipe(server).pipe(stream);
var client = require('pipe-channels').createClient();
var payload = { token: 'this is a token'};
// ask for a channel, passing in any arbitrary payload:
client.channel(payload, function(err, channel) {
if (err) {
console.error('could not get a channel because', err.message);
} else {
// now I have a channel, an object stream that I can pipe
stream.pipe(channel).pipe(stream);
}
});
ISC