Platform Events in Lightning Component

Platform Events are used to deliver secure, scalable, and customizable notification within Salesforce or external app. Platform Event is based on Event-Driven Architecture. This is built in real time integration patterns in the Salesforce Platform which helps to reduce point-to-point integration.

WHEN TO USE PLATFORM EVENTS?

  1. Platform events are very useful when you want to communicate Salesforce with any external system.
  2. Platform events are also used to communicate between two lightning applications.
  3. Platform events are very useful when you want to communicate between two visualforce pages.

SUBSCRIPTION IN PLATFORM EVENT: 

  1. Apex triggers receive event notifications.
  2. write an “after insert” Apex trigger on the event object to subscribe to incoming events.
  3. Triggers receive event notifications from various sources—whether they’re published through Apex or APIs.
  4. Visualforce and Lightning component apps receive events through CometD.
  5. CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet.
  6. In an external app, you subscribe to events using CometD as well.

CREATING PLATFORM EVENT OBJECT

Setup —-> Platform Event —-> New

Create Notification__e Platform event

Creation of Platform Event Notification__e

Create a Lightning Component to display toast based on Platform event Creation

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.

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

Add this lightning component in lightning app utiliy bar

Once Platform event record get Created, A toast message is displayed

Source : https://sfdcrecipes.com/2019/04/13/platform-events-in-lightning-component/

Leave a Comment