1. pubsub
PubSub for Node and the Browser.
pubsub
Package: pubsub
Created by: wski
Last modified: Fri, 24 Jun 2022 23:47:22 GMT
Version: 3.2.1
License: MIT
Downloads: 1,730
Repository: https://github.com/wski/pubsub

Install

npm install pubsub
yarn add pubsub

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.

 require('pubsub');

Usage

Join a channel

 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

 // You can leave the channel by passing the uuid provided in join.
PubSub
  .leave('general', uuid)
  .then(() => {
    // successfully left channel
  });

Publish to a channel

 PubSub
  .publish('general', {test: 'passed'})
  .then(() => {
    // message sent to all subscribers
  });

Enable cross tab communication (for web applications)

 PubSub.setFlag('crossTabEnabled', true);

Enable history

 PubSub.setFlag('historyEnabled', true);

Increase history limit (default 15)

 PubSub.setFlag('historyLength', 30);

Get history

 PubSub.history('general', 15); // Returns 15 messages from history

Example

JSFiddle.net example

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code