UNPKG

pubsub

Version:

PubSub for Node and the Browser.

68 lines (54 loc) 1.36 kB
## Installation Install and save the pubsub module. `npm i --save pubsub` Or use with script tag `<script type="text/javascript" src="https://gitlab.me/wski/pubsub/raw/master/dist.js"></script>` Then simply require it at the root level of your project. ```javascript require('pubsub'); ``` ## Usage Join a channel ```javascript PubSub.join('general', (message, uuid) => { // Upon joining you will receive a uuid without a message. // Other times, you will be sent a message, and a uuid. if (message) console.log(`${uuid} recived message`, message); }); ``` Leave a channel ```javascript // You can leave the channel by passing the uuid provided in join. PubSub .leave('general', uuid) .then(() => { // successfully left channel }); ``` Publish to a channel ```javascript PubSub .publish('general', {test: 'passed'}) .then(() => { // message sent to all subscribers }); ``` Enable cross tab communication (for web applications) ```javascript PubSub.setFlag('crossTabEnabled', true); ``` Enable history ```javascript PubSub.setFlag('historyEnabled', true); ``` Increase history limit (default 15) ```javascript PubSub.setFlag('historyLength', 30); ``` Get history ```javascript PubSub.history('general', 15); // Returns 15 messages from history ``` ## Example [JSFiddle.net example](https://jsfiddle.net/whqbec73/11/)