conversation-stream

json conversations over a stream

Conversation-Stream

Conversation-Stream allows you to have a JSON conversation over a stream.

npm install conversation-stream

To listen for messages you just need to listen to the message event

var cs = require('conversation-stream')
var conversation = cs();
 
fromStream.pipe(conversation).pipe(fromStream);
 
conversation.on('message', function(messagerespond) {
    respond(null, {echo:message}); // send a reply back if you want to 
});

To send messages you call conversation.send(message) To send messages and wait for a reply you call conversation.send(message, callback)

var cs = require('conversation-stream');
var conversation = cs();
 
toStream.pipe(conversation).pipe(toStream);
 
conversation.send('hello', function(errreply) {
    console.log(err, reply); // prints {echo:'hello'} 
});
 
conversation.send('world', function(errreply) {
    console.log(err, reply); // prints {echo:'world'} 
});
 

Lets try to setup a simple server

var cs = require('conversation-stream');
var net = require('net');
 
net.createServer(function(socket) {
    var conversation = cs();
 
    socket.pipe(conversation).pipe(socket);
 
    conversation.on('message', function(messagerespond) {
        conversation.send({server:message}, respond);
    });
}).listen(9000);

To start a conversation to the server we need to create a socket to the server and pipe our message to that.

var socket = net.connect(9000);
var conversation = cs();
 
socket.pipe(conversation).pipe(socket);
 
conversation.on('message', function(messagerespond) {
    respond(null, {client:message});
});
 
conversation.send('echo me please', function(errreply) {
    console.log(err, reply);  // prints {client:{server:'echo me please'}} 
});
conversation.send('echo me please again', function(errreply) {
    console.log(err, reply); // prints {client:{server:'echo me please again'}} 
});

If we wanted to use tls instead of tcp we could just have implemented the above example using tls streams. We could even use WebSockets using shoe.