client-broadcast-pub-sub
v1.0.3
Published
This is a module for broadcasting events and listening to events in same browser context
Downloads
9
Readme
hsd-client-broadcast
A library to allow communication between browsing contexts. It can be used to detect user-actions in other tabs like when user logs out. A user action change in one tab will broadcast the message which is listened by other browser tabs and perform the required operation.
Install
npm Command
Example (Same Broadcast and Listner event):
import {BroadCastChannel} from 'client-broadcast-pub-sub';
const broadcastChannel = new BroadCastChannel();
broadcastChannel.registerLogout(logout);
broadcastChannel.broadCastLogout();
export function logout(): void {
window.location.href = `/logout`;
}
Example (Different Broadcast and Listner event):
import broadcastChannel from 'client-broadcast-pub-sub';
const broadcastChannel = new BroadCastChannel();
broadcastChannel.registerLogout(SomeFunctionOne,SomeFunctionTwo);
broadcastChannel.broadCastLogout();
export function SomeFunctionOne(): void {
// Code
}
export function SomeFunctionTwo(): void {
//Code
}
Method
registerLogout(broadcastFunc,listenerFunc)
Register Broadcast and Listener events.
| name | type | data type | need | description |
| ------------ | ------ | --------------- | ---- | ----------------- |
| broadcastFunc| param | Fnction
| yes | broadcast event name |
| listenerFunc | param | Undefined/Function
| no | callback function |
broadCastLogout()
Broadcast the event registered in registerLogout
Build