Example: Subscribe to and Replay Events Using a Lightning Component

Subscribe to event streaming channels with the empApi component in your Lightning web component or Aura component. The empApi component provides access to methods for subscribing to a streaming channel and listening to event messages.

All types of streaming events are supported, including:

  • Platform events
  • Change Data Capture events
  • PushTopic events (legacy)
  • Generic events (legacy)

The empApi component uses a shared CometD-based Streaming API connection, enabling you to run multiple streaming apps in the browser for one user. The connection isn’t shared across user sessions.

The concurrent CometD client limit applies to the empApi component. Each logged-in user using empApi counts as one concurrent client. The empApi component isn’t recommended for apps or sites that are used by a large number of users, such as Experience Cloud sites, because the limit can be reached. This limit is shared with other CometD clients. For more information, see Platform Event Allocations in the Platform Events Developer Guide.

As of Spring ’19 (API version 45.0), you can build Lightning components using two programming models: the Lightning Web Components model, and the original Aura Components model. Lightning web components are custom HTML elements built using HTML and modern JavaScript. Lightning web components and Aura components can coexist and interoperate on a page.

Note

Subscribe in a Lightning Web Component

To use the empApi in your Lightning web component, import its methods from the lightning/empApi module as follows.

import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } 
    from 'lightning/empApi';

Then call the imported methods in your JavaScript code.

For an example of how to use the lightning/empApi module and a complete reference, see the lightning-emp-api documentation in the Lightning Component Library.

Subscribe in an Aura Component

To use the empApi methods in your Aura component, add the lightning:empApi component inside your custom component and assign an aura:id attribute to it.

<lightning:empApi aura:id="empApi"/>

Then in the client-side controller, add functions to call the empApi methods.

For an example of how to use the lightning:empApi component and a complete reference, see the lightning:empApi documentation in the Lightning Component Library.