Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c7ae4b9a3e
Fetching contributors…

Cannot retrieve contributors at this time

file 95 lines (64 sloc) 1.84 kb

emit-stream

turn event emitters into streams and streams into event emitters

build status

example

write a server that streams an event emitter's events to clients:

var emitStream = require('emit-stream');
var EventEmitter = require('events').EventEmitter;
var net = require('net');

var server = (function () {
    var ev = createEmitter();

    return net.createServer(function (stream) {
        emitStream(ev).pipe(stream);
    });
})();
server.listen(5555);

function createEmitter () {
    var ev = new EventEmitter;
    setInterval(function () {
        ev.emit('ping', Date.now());
    }, 2000);

    var x = 0;
    setInterval(function () {
        ev.emit('x', x ++);
    }, 500);

    return ev;
}

then re-constitute the event-emitters on the client:

var emitStream = require('event-stream');
var net = require('net');

var stream = net.connect(5555);
var ev = emitStream(stream);

ev.on('ping', function (t) {
    console.log('# ping: ' + t);
});

ev.on('x', function (x) {
    console.log('x = ' + x);
});

methods

var emitStream = require('emit-stream')

emitStream(x)

If x is a stream, returns an event emitter from emit.toStream(x).

Otherwise returns a stream from emit.fromStream(x).

emitStream.toStream(emitter)

Return a stream from the EventEmitter emitter.

The arguments are serialized with JSONStream.

emitStream.fromStream(stream)

Return an EventEmitter from stream.

The stream is parsed with JSONStream to re-create the event emitter arguments.

install

With npm do:

npm install emit-stream

license

MIT

Something went wrong with that request. Please try again.