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?
- Platform events are very useful when you want to communicate Salesforce with any external system.
- Platform events are also used to communicate between two lightning applications.
- Platform events are very useful when you want to communicate between two visualforce pages.
SUBSCRIPTION IN PLATFORM EVENT:
- Apex triggers receive event notifications.
- write an “after insert” Apex trigger on the event object to subscribe to incoming events.
- Triggers receive event notifications from various sources—whether they’re published through Apex or APIs.
- Visualforce and Lightning component apps receive events through CometD.
- CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet.
- 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
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/