Platform Events in Aura Component Salesforce

by Rijwan Mohmmed

Hello friends, today we will discuss How to use Platform Events in Aura Component Salesforce. Here we will publish the events in Apex and Subscribe to the Aura component (Lightning Component).

Also, check this: Platform Events in LWC Salesforce

platform-events-in-Lightning Component-salesforce-output-techdicer

Key Highlights :

  1. With platform events, you can program your apps in a standard way and use an event-based model.
  2. You can also publish the events in flow and subscribe to Aura Component.
  3. Platform event is used to send parameters from one component to another that have no relationship.
  4. Like in my case I fire the event from apex class and will subscribe by Lightning component.

Process & Code :

Step 1: First of all we will create a Platform event and its fields

Setup > Platform Events > click New Platform Event



Step 2: In this step, I will create an Apex class to publish the platform event.

PlatformEventCtrl.cls :

public class PlatformEventCtrl {
    public static void publishEvent(){
        Techdicer_Event__e event = new Techdicer_Event__e();
        event.message__c = 'test';
        event.recordId__c = '0016F000041zsTDQAY';
        event.status__c = 'Success';

Step 3: Here we will create an Aura component for Subscribe the event and show data which send by the platform event.

PlatformEventAura.cmp :

<aura:component  implements="flexipage:availableForAllPageTypes" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="status" type="String" />
    <aura:attribute name="message" type="String" />
    <aura:attribute name="recordId" type="String" />
    <lightning:empApi aura:id="empApi" />
    <lightning:card title="Platform Events in Aura Component Salesforce" iconName="standard:account">
        <div class="slds-p-horizontal_small">
            <p>Status : {!v.status}</p>
            <p>Message : {!v.message}</p>
            <p>Record Id : {!v.recordId}</p>

PlatformEventAuraController.JS :

	doInit : function(component, event, helper) {
        var channel = '/event/Techdicer_Event__e';
        const replayId = -1;
        const empApi = component.find("empApi");
        //A callback function that's invoked for every event received
        const callback = function (message) {
            var obj =;
            component.set("v.status", obj.Status__c);
            component.set("v.message", obj.message__c);
            component.set("v.recordId", obj.recordId__c);
            //fire toast message
            var toastEvent = $A.get("e.force:showToast");
                "mode" : 'sticky',
                "title" : "Success",
                "message" : obj.message__c,
                "type" : "success"
        // Subscribe to the channel and save the returned subscription object.
        empApi.subscribe(channel, replayId, callback).then(function(newSubscription) {
            //console.log("Subscribed to channel 1" + channel);
        const errorHandler = function (message) {
            console.error("Received error ", JSON.stringify(message));
        //A callback function that's called when an error response is received from the server for the handshake, connect, subscribe, and unsubscribe meta channels.

Output :

platform-events-in-Lightning Component-salesforce-output-techdicer

Reference :

  1. Platform Event in Aura Component
What’s your Reaction?

You may also like

Leave a Comment