emit-stream3

1.0.0 • Public • Published

emit-stream3

Turn event emitters into streams and streams into event emitters. Streams3 version of emit-stream.

build status

Usage

Borrowing from the original README, let's write a server that streams an event emitter's events to clients:

const emitStream = require('emit-stream3')
const JSONStream = require('JSONStream')
const net = require('net')
const EventEmitter = require('events').EventEmitter
 
const server = (function () {
  const ev = createEmitter()
  return net.createServer(stream => 
    emitStream(ev)
      .pipe(JSONStream.stringify())
      .pipe(stream)
  )
})()
server.listen(5555)
 
function createEmitter () {
  const ev = new EventEmitter
  setInterval(() => ev.emit('ping', Date.now()), 2000)
  let x = 0
  setInterval(() => ev.emit('x', x ++), 500)
  return ev
}

Then re-constitute the event emitters on the client:

const emitStream = require('emit-stream3')
const net = require('net')
const stream = net.connect(5555).pipe(JSONStream.parse([true]))
const ev = emitStream(stream)
 
ev.on('ping', t => console.log('# ping: ' + t))
ev.on('x', x => console.log('x = ' + x))

Outputting:

$ node example/emit.js 
x = 0 
x = 1 
x = 2 
x = 3 
# ping: 1346116850523
x = 4 
x = 5 
^C 

API

const emitStream = require('emit-stream3')

emitStream(x)

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

emitStream.toStream(emitter)

Return a stream from the EventEmitter emitter.

The 'data' emitted by this stream will be an array. Serialization is up to you. I recommend JSONStream for most purposes.

emitStream.fromStream(stream)

Return an EventEmitter from stream.

The 'data' written to this stream should be an array, like JSONStream creates.

Installation

With npm do:

npm install emit-stream3

License

MIT

Dependencies (1)

Dev Dependencies (2)

Package Sidebar

Install

npm i emit-stream3

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

6.86 kB

Total Files

10

Last publish

Collaborators

  • akiva