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 1,000,000+ packages pre-installed, including psst with all npm packages installed. Try it out:

var psst = require("psst")

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

psst v2.0.0

A small Pub/Sub library that uses subscriptions rather than callbacks.

psst.js

This is yet another small Pub/Sub library, however unlike many other implementations, the subscribe method returns an object that describes the channel and callback.

Installing

$ npm install psst
var psst = require('psst');

or

<script src="https://rawgit.com/flesch/psst.js/master/index.js"></script>

Using

psst.js supports both a single global listener (psst.on), or many independent listeners (new psst()).

var listener = new psst();

Subscribe/On

var subscription = listener.on('topic', function(msg){
  console.log(msg);
});
var subscription = psst.on('topic', function(msg){
  console.log(msg);
});

subscription is a unique object that contains the channel and callback.

Subscribe Once

var subscription = listener.once('topic', function(msg){
  console.log(msg);
});
var subscription = psst.once('topic', function(msg){
  console.log(msg);
});

This subscription is removed from the subscriber's list after the callback has been executed.

Unsubscribe/Off

listener.off(subscription);
psst.off(subscription);

Publish/Emit

listener.emit('topic', 'Hello World!');
psst.emit('topic', 'Hello World!');

License

Released under the MIT License: http://flesch.mit-license.org

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