Permalink
Browse files

fix emit example to use JSONStream itself

  • Loading branch information...
1 parent 99da7d1 commit 358321ed6dbc90966a236d04ab9eca156b9d18c4 @substack committed Aug 28, 2012
Showing with 41 additions and 13 deletions.
  1. +8 −3 example/emit.js
  2. +3 −2 package.json
  3. +30 −8 readme.markdown
View
@@ -1,19 +1,24 @@
var emitStream = require('../');
var EventEmitter = require('events').EventEmitter;
+var JSONStream = require('JSONStream');
var net = require('net');
var server = (function () {
var ev = createEmitter();
- var s = emitStream(ev);
return net.createServer(function (stream) {
- s.pipe(stream);
+ emitStream(ev)
+ .pipe(JSONStream.stringify())
+ .pipe(stream)
+ ;
});
})();
server.listen(5555);
server.on('listening', function () {
- var stream = net.connect(5555);
+ var stream = net.connect(5555)
+ .pipe(JSONStream.parse([true]))
+ ;
var ev = emitStream(stream);
ev.on('ping', function (t) {
View
@@ -9,10 +9,11 @@
"test" : "test"
},
"dependencies" : {
- "JSONStream" : "~0.3.1"
+ "through" : "~0.1.4"
},
"devDependencies" : {
- "tap" : "~0.3.0"
+ "tap" : "~0.3.0",
+ "JSONStream" : "~0.3.1"
},
"scripts" : {
"test" : "tap test/*.js"
View
@@ -10,18 +10,23 @@ write a server that streams an event emitter's events to clients:
``` js
var emitStream = require('emit-stream');
-var EventEmitter = require('events').EventEmitter;
+var JSONStream = require('JSONStream');
var net = require('net');
var server = (function () {
var ev = createEmitter();
return net.createServer(function (stream) {
- emitStream(ev).pipe(stream);
+ emitStream(ev)
+ .pipe(JSONStream.stringify())
+ .pipe(stream)
+ ;
});
})();
server.listen(5555);
+var EventEmitter = require('events').EventEmitter;
+
function createEmitter () {
var ev = new EventEmitter;
setInterval(function () {
@@ -43,7 +48,9 @@ then re-constitute the event-emitters on the client:
var emitStream = require('event-stream');
var net = require('net');
-var stream = net.connect(5555);
+var stream = net.connect(5555)
+ .pipe(JSONStream.parse([true]))
+;
var ev = emitStream(stream);
ev.on('ping', function (t) {
@@ -55,6 +62,20 @@ ev.on('x', function (x) {
});
```
+***
+
+```
+$ node example/emit.js
+x = 0
+x = 1
+x = 2
+x = 3
+# ping: 1346116850523
+x = 4
+x = 5
+^C
+```
+
# methods
``` js
@@ -71,16 +92,17 @@ Otherwise returns a stream from `emit.fromStream(x)`.
Return a stream from the EventEmitter `emitter`.
-The arguments are serialized with
-[JSONStream](http://github.com/dominictarr/JSONStream).
+The `'data'` emitted by this stream will be array data.
+Serialization is up to you. I recommend
+[JSONStream](http://github.com/dominictarr/JSONStream)
+for most purposes.
## 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.
+The `'data'` written to this stream should be an array, like
+[JSONStream](http://github.com/dominictarr/JSONStream) creates.
# install

0 comments on commit 358321e

Please sign in to comment.