Emp API

lightning:empApi

Work with the EmpJs Streaming API library, which subscribes to a streaming channel and listens to event messages using a shared CometD connection for a single user session. This component is supported only in desktop browsers. This component requires API version 44.0 or later.

For Aura components only. For LWC development, use lightning-emp-api.

For Use In

Lightning Experience

The lightning:empApi component provides access to methods for subscribing to a streaming channel and listening to event messages. All streaming channels are supported, including channels for platform events, PushTopic events, generic events, and Change Data Capture events. The lightning:empApi component uses a shared CometD connection, enabling you to run multiple streaming apps in a single browser session. The connection isn’t shared across user sessions in other browsers. The lightning:empApi component only supports one user per browser. Multiple user sessions aren't supported in one browser.

To call the component's methods, add the lightning:empApi component inside your custom component and assign an aura:id attribute to it.

This example subscribes to a channel when you click the Subscribe button. The channel is specified in an input component. The default value provided is an example platform event channel. Replace the value with the desired channel name. A callback function on the subscribe() call prints the payload of each received event to the console. To view the event messages, enable your browser console view. The Unsubscribe button lets you stop the subscription and stop receiving event notifications.

Add the following client-side controller functions. They are called by the Subscribe and Unsubscribe buttons. Each controller function calls the corresponding subscribe or unsubscribe method on the empApi component.

The lightning:empApi component is supported only in desktop browsers with web worker or shared worker support. It is not supported in the Salesforce mobile app. For more information about web workers and browser support, see the Web Workers W3C specification and Using Web Workers in the Mozilla Developer Network documentation.

You can add the lightning:empApi component only on the main window of a page. You can't add the lightning:empApi component on a child window. For example, in a screen flow, you can add the lightning:empApi component only on the main screen but not on a button in a screen flow. Similarly, you can’t add the lightning:empApi component in the utility bar pop-out window. Another example is a Visualforce page that contains a top-level window and child iframe windows. In this case, the lightning:empApi component must be on the top-level window.