---
schema_version: "1"
id: 0a3b3d4c630e416dcd7b177325ef3d4ff5d4ac1865ecab1d5c5fe4f5e283658c
title: Omnistudio Pubsub
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/lightning-base-components/lightning-design-system/guides/lwc/components/lightning/omnistudioPubsub/lightning-omnistudio-pubsub.md
canonical_url: https://developer.salesforce.com/docs/platform/lightning-component-reference/guide/lightning-omnistudio-pubsub.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 7016eab9ae61c1e724714205a18bfcc5d417292383209c354ad816391e4008bf
products:
  - Lightning Component Reference
---
# Omnistudio Pubsub

**`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](https://help.salesforce.com/s/articleView?id=xcloud.os_trigger_a_pubsub_event_from_an_action_on_a_flexcard_28089.htm), or [communicate from an Omniscript to a Lightning web component](https://help.salesforce.com/s/articleView?id=xcloud.os_communicate_with_omniscript_from_a_lightning_web_component_18222.htm). 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.

```javascript
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](https://developer.salesforce.com/docs/platform/lwc/guide/use-config-for-app-builder.html). To make your component available in Experience Builder, see [Configure a Component for Experience Builder](https://developer.salesforce.com/docs/platform/lwc/guide/use-config-for-community-builder.html).

This example uses the `lightning/omnistudioPubsub` module to register, unregister, and fire a test event dynamically.

```javascript
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"}

:::::

::::::
