Skip to content
Browse files

dox

  • Loading branch information...
1 parent e67c4a4 commit c3c08236ec4fb52a173978bcdd151960b2fc2e39 @substack committed Aug 21, 2012
Showing with 93 additions and 0 deletions.
  1. +93 −0 readme.markdown
View
93 readme.markdown
@@ -0,0 +1,93 @@
+# emit-stream
+
+turn event emitters into streams and streams into event emitters
+
+# example
+
+write a server that streams an event emitter's events to clients:
+
+``` js
+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:
+
+``` js
+var emitStream = require('event-stream');
@coltrane
coltrane added a note Aug 31, 2012

I think this should be:

var emitStream = require('emit-stream');

s/event-/emit-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+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
+
+``` js
+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](http://github.com/dominictarr/JSONStream).
+
+## emitStream.fromStream(stream)
+
+Return an EventEmitter from `stream`.
+
+The `stream` is parsed with
+[JSONStream](http://github.com/dominictarr/JSONStream)
+to re-create the event emitter arguments.
+
+# install
+
+With [npm](http://npmjs.org) do:
+
+```
+npm install emit-stream
+```
+
+# license
+
+MIT

0 comments on commit c3c0823

Please sign in to comment.
Something went wrong with that request. Please try again.