lightning-omnistudio-pubsub
Provides a lightweight publish-subscribe mechanism for sibling Lightning web components to communicate without direct references.
For Use In
Lightning Experience, Experience Builder Sites
::::::tabset{tabs='[ "Develop", "Specifications"]'}
:::::tab
Share data and pass messages between components that don’t have a parent-child relationship using the lightning/omnistudioPubsub module. Using this module, you can register, unregister, and fire events dynamically.
In Omnistudio, you can trigger a pubsub event from an action on a Flexcard, or communicate from an Omniscript to a Lightning web component. Omnistudio is available to select Industry Cloud customers.
To use Omnistudio PubSub directly in your custom Lightning web component, use the lightning/omnistudioPubsub module.
The lightning/omnistudioPubsub module is a wrapper for the Omnistudio PubSub property.
To use the pubsub functionality, import pubsub from the lightning/omnistudioPubsub module in the component.
import pubsub from "lightning/omnistudioPubsub";
// Register a callback
pubsub.register("eventName", callbackObject);
// Fire an event
pubsub.fire("eventName", action, payload);
// Unregister a callback
pubsub.unregister("eventName", callbackObject);
This utility component doesn’t have a template and can’t be directly accessed via Lightning App Builder. A custom component that uses the utility component can be accessed via Lightning App Builder. To make your component available in Lightning App Builder, see Configure a Component for Lightning App Builder. To make your component available in Experience Builder, see Configure a Component for Experience Builder.
This example uses the lightning/omnistudioPubsub module to register, unregister, and fire a test event dynamically.
import omnistudioPubsub from "lightning/omnistudioPubsub";
export default class TestComponent extends LightningElement {
connectedCallback() {
this.handleMessage = {
testevent: this.handleTestEvent.bind(this),
testeventnew: this.handleTestEventNew.bind(this),
};
omnistudioPubsub.register("testchannel", this.handleMessage);
}
handleTestEvent(event) {
//Some logic here
}
handleTestEventNew(event) {
//Some logic here
}
disconnectedCallback() {
omnistudioPubsub.unsubscribe("testchannel", this.handleMessage);
}
}
Public Methods
register(eventName, callbackobj)
Register a set of event handlers to a specific channel.
| Parameter | Type | Description |
|---|---|---|
| eventName | string | The name of the event to register for |
| callbackobj | object | The callback object to register |
unregister(eventName, callbackobj)
Unregister your set of event handlers from a channel. To avoid memory leaks or potential errors, always unregister event handlers when a component is disposed or disconnected. In addition, to properly unregister event handlers, pass both the channel name and instance of your event handler objects.
| Parameter | Type | Description |
|---|---|---|
| eventName | string | The name of the event to unregister from |
| callbackobj | object | The callback object to unregister |
fire(eventName, action, payload)
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.
| Parameter | Type | Description |
|---|---|---|
| eventName | string | The name of the event to fire |
| action | string | The action to perform |
| payload | object | The payload data for the event |
:::::
:::::tab ::component-specification{component="omnistudioOmniscript" model="lwc" namespace="lightning"}
:::::
::::::