emit.io
Turn a node stream into a socket.io-like 2-way messaging channel. Turn an event emitter into a stream. Multiple stream destinations and multiple listeners are supported.
example - 2 way messaging
var emitIO = var emitter = var outputStream = socketoutputStreamemitteremitter
var emitIO = require('emit.io')
Usage In the following section references to socket
are assumed to be a duplex socket like a tcp connection or websocket connection or any other node duplex stream. You will have to add some sort of JSON encoding logic, that is left out for brevity. (JSONStream or mux-demux are recommended)
Stream->Emitter
Just pass emitIO
a stream and it will return an event emitter that will listen on the stream for events
var emitter =
Emitter->Stream
Pass emitIO
an emitter and receive a stream that will channel the events. For 2-way communication, pass in the emitter that was returned by the stream->emitter call.
New Emitter
var ev = var outStream = socketoutStream
Link existing emitter into an inputStream
To hook an existing event emitter into an input and output channel do:
var ev = var outStream = socketoutStream
Hook new emitters into existing outgoing streams
You can get a new emitter from an incoming stream and hook it into an existing outgoing stream
var outStream = var newEmitter = // now newEmitter will pipe its communication down the same channel as someExistingEmitter
Send events down multiple streams
To send events from a single emitter down multiple streams do
var MuxDemux = var net = var EmitFactory = net
which prints
server emitter 0 got msg hello from the client!server emitter 1 got msg hello from the client!client got: server msg from emitter0client got: server msg from emitter1