npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

emit.io

v0.2.3

Published

turn emitters to streams and streams to emitters. Event branching and 2-way communication

Downloads

185

Readme

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 = require('emit.io')
var emitter = emitIO(inputStream)
var outputStream = emitIO(emitter)
socket.pipe(inputStream)
outputStream.pipe(socket)
emitter.on('remote-msg', function () {})
emitter.emit('send-to-remote', {data: 3})

Usage var emitIO = require('emit.io')

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 = emitIO(inStream)

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 = emitIO(inStream)
var outStream = emitIO(ev)
socket.pipe(inStream)
outStream.pipe(socket)

Link existing emitter into an inputStream

To hook an existing event emitter into an input and output channel do:

var ev = new EventEmitter()
var outStream = emitIO(ev)
emitIO(inStream, ev)
socket.pipe(inStream)
outStream.pipe(socket)

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 = emitIO(someExistingEmitter)
var newEmitter = emitIO(inStream)
emitIO(newEmitter, outStream)   // 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 = require('mux-demux')
var net = require('net')
var EmitFactory = require('../../.')

net.createServer(function (con) {
  var emitIO = EmitFactory()
  var mx = MuxDemux(function (stream) {
             var ev = emitIO(stream)
             var emitStream = emitIO(ev)
             emitStream.pipe(stream)

             ev.on('client-msg', function (msg) {
               console.log('server emitter', ev.eID, 'got msg', msg)
             })
             ev.emit('server-msg', 'server msg from emitter ' + ev.eID)
           })
  con.pipe(mx).pipe(con)

}).listen(8642, function () {
  var emitIO = EmitFactory()
  var con = net.connect(8642)
    , mx = MuxDemux()
  con.pipe(mx).pipe(con)
  // you can easily create a new emitter and stream for each
  // mx stream, but we are going to have more fun and wire
  // both streams into one emitter, and let that one emitter
  // send events down both streams
  var s1 = mx.createStream()
  var s2 = mx.createStream()
  var ev = emitIO(s1)
  emitIO(s2, ev) // hook s2 into our emitter
  var emitStream = emitIO(ev) // get an output stream
  emitStream.pipe(s1) // pipe output to s1
  emitStream.pipe(s2) // pipe output to s2

  ev.emit('client-msg', 'hello from the client!')

  ev.on('server-msg', function (msg) {
    console.log('client got:', msg)
  })

})

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 emitter0
client got: server msg from emitter1