Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including ev-pubsub with all npm packages installed. Try it out:

var evPubsub = require("ev-pubsub")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

ev-pubsub v2.4.0

simplistic pubsub

ev-pubsub

asynchronous publish/subscribe module.

npm is flooded with similar modules but I couldn't find any that satisfy all of these criteria:

  • doesn't generate memory garbage
  • favors factory pattern over terrible javascript classes
  • events published in next event loop tick
  • works in both commonjs (require) and es modules (import)
  • simple (< 100 lines of code)

so, here we are.

including

import pubsub from 'ev-pubsub'  // modern es modules approach

// *OR*

const pubsub = require('ev-pubsub') // commonjs (node) approach

basic usage

const a = pubsub()

a.subscribe('red-light', function (arg1, arg2, arg3) {
  console.log('hello from 1:', arg1)
})

a.subscribe('red-light', function (arg1, arg2, arg3) {
  console.log('hello from 2:', arg1)
})

// all arguments after the first will be sent to subscribers
a.publish('red-light', 'val a', 'val b', 'val c')

/*
prints:

hello from 1: val a
hello from 2: val b
*/

event loop considerations

many pubsub implementations immediately invoke published events.

A problem with this approach is you might end up missing events if they happen before subscribing to them.

ev-pubsub gets around this by invoking published events in the next event tick (asynchronously.) example:

function myFunc (a) {
  console.log('test', a)
}


const mine = pubsub()
mine.publish('some-event', 'omg it happened!')

mine.subscribe('some-event', function (msg) {
  console.log('event message:', msg)
})

/*
prints:

event message: omg it happened!
*/

In some libraries you might expect this to print nothing, because you didnt subscribe to the some-event event until after it was published to. Because this library publishes events on the next tick, you'll be able to set up subscribers without worrying about losing messages due to ordering.

unsubscribe

You may also unregister event handlers if you need to cleanup or prevent memory leaks:

function myFunc (a, b) {
  console.log('yo!', a, b)
}

const a = pubsub()

a.subscribe('some-event', myFunc)

a.publish('some-event', 'zero arg', 'one arg')


// simulate some time passing
setTimeout(function () {
  a.unsubscribe('some-event', myFunc)
  a.publish('some-event', 'yay!')
}, 1000)

/*
prints:

yo! zero arg one arg
*/

unsubscribe all handlers for a given topic

function myFunc (a) {
  console.log('yo!', a)
}

function myFunc2 (a) {
  console.log('yo2!', a)
}

const a = pubsub()

a.subscribe('some-event', myFunc)

a.subscribe('some-event', myFunc2)

a.publish('some-event', 'ayy')

a.unsubscribe('some-event')

console.log('unsubscribed all...')
a.publish('some-event', 'beee')

/*
prints:

yo! ayy
yo2! ayy
unsubscribed all...
*/

once

You may want to only subscribe to one event:

function myFunc (a) {
  console.log('yo!', a)
}

const a = pubsub()

a.once('some-event', myFunc)

a.publish('some-event', '15')
a.publish('some-event', '16')


/*
prints:

yo! 15
*/
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free