request-stream

json request-response over a stream

request-stream

Send json request and responses over a stream

npm install request-stream

Lets try to setup a simple server

var rs = require('request-stream');
var net = require('net');
 
net.createServer(function(socket) {
    var r = rs();
 
    socket.pipe(r).pipe(socket);
 
    r.on('request', function(requestrespond) {
        respond(null, {echo:request});
    });
}).listen(9000);

Since the server is a tcp server we just need to create a tcp socket and pipe our request to that.

var socket = net.connect(9000);
var r = rs();
 
socket.pipe(r).pipe(socket);
 
r.request('echo me please', function(errreply) {
    console.log(err, reply);
});

Since r is just a stream we can pipe it to any kind of transport (even WebSockets using shoe).