You are here:
Pubsub Component (Managed Package)
For the managed package runtime, the Pubsub Component enables data sharing and passing messages between components that aren’t ancestors. Multiple components can register and receive data from the same channel.
This information is for Omnistudio for Managed Packages. For Omnistudio on standard runtime, see Omnistudio Help.
This component exposes 4 functions:
-
register
-
unregister
-
fire
-
shouldExecuteCallbackHandler.shouldExecuteCallback
To use your component as a listener, add your handler object with the events it wants to handle as a property on your LWC. Then, unregister the handlers in the disconnectedCallback. It’s important to unregister components to ensure that you don’t leak DOM components into memory or cause potential errors by continuing to receive and process events on disconnected and disposed LWC components.
import {LightningElement} from "lwc";
import pubsub from "vlocity_ins/pubsub";
export default class MyLwc extends LightningElement {
connectedCallback() {
this._myPubSubHandlers = {
onresult: this.myResultHandler.bind(this)
};
pubsub.register(this.channel, this._myPubSubHandlers);
}
disconnectedCallback() {
pubsub.unregister(this.channel, this._myPubSubHandlers);
}
myResultHandler(event) {
// handle the event.
}
}Register
Register a set of event handlers to a specific channel.
this._myPubSubHandlers = {
onresult: this.myResultHandler.bind(this)
};
pubsub.register(this.channel, this._myPubSubHandlers);Unregister
Unregister your set of event handlers from a channel. Always unregister event handlers when a component is disposed or disconnected to avoid memory leaks or potential errors. In addition, to be properly unregistered event handlers, pass both the channel name and instance of your event handler objects.
disconnectedCallback() {
pubsub.unregister(this.channel, this._myPubSubHandlers);
}Fire
Fire an event over a specific channel to all registered handlers. The payload can be any object. However, make sure your handlers expect that particular structure. For example, if you intend to send a JSON object with various keys, your handlers should know what those keys are.
pubsub.fire("Channel Name", "Event name", { /* payload */ });shouldExecuteCallbackHandler.shouldExecuteCallback
Override this function to implement a check before executing the registered callback. This function returns true if you want the callback to execute or return false. The default value returned is true.
pubsub.shouldExecuteCallbackHandler.shouldExecuteCallback = (callback, payload) => {
//implement the function to return true if you want the callback to execute else return false.
if (condition) { //some condition.
return true;
} else {
return false;
}
}NOTE: This is a global function that applies to all users of the pubsub fire event on the same page.
- Pubsub Component ReadMe Previous Releases (Managed Package)
For the managed package runtime, this page contains the Pubsub Component ReadMe for older releases.
