OfflineBrowserPubSub is a simple JavaScript pub-sub system inspired by tabsub using the browsers local storage to send messages between windows and tabs.
Install with NPM:
npm i --save offline-browser-pub-sub
Install with Yarn:
yarn add offline-browser-pub-sub
// Require offline-browser-pub-sub
import browserPubSub from 'offline-browser-pub-sub'
// Subscribe to a topic
const token = browserPubSub.subscribe('my-topic', message => {
console.log(message)
})
// Publish to all tabs
browserPubSub.publish('my-topic', 'my-message')
// Publish without notifying the same tab
browserPubSub.publish(
'my-topic',
{
text: 'my-complex-message',
from: 'me',
to: 'a friend',
},
{ notifySameTab: false },
)
// Unsubscribe from a topic
browserPubSub.unsubscribe(token)
Browser Tab Pong: Simple pong game across multiple browser windows.
After installing the dependencies with yarn install
, run yarn dev
to build offline-browser-pub-sub with webpack and serve the example at http://localhost:5000
.
To install this library onto your local machine, run npm install
. To release a new version, run npm version <update_type>
to update the version number in package.json, and then run npm publish
.
The npm package is available as open source under the terms of the MIT License.