Platform Events in Aura Component Salesforce

by Rijwan Mohmmed
0 comment
platform-events-in-aura-component-salesforce-techdicer

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-lwc-salesforce-output-techdicer
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

platform-events-in-aura-component-salesforce-platform-event-object-techdicer

platform-events-in-Lightning-component-salesforce-platform-event-fields-techdicer

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';
        EventBus.publish(event);
    }
}

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>
        </div>
    </lightning:card>
</aura:component>

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 = message.data.payload;
            console.log(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");
            toastEvent.setParams({
                "mode" : 'sticky',
                "title" : "Success",
                "message" : obj.message__c,
                "type" : "success"
            });
            toastEvent.fire();
        };
        
        // 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.
        empApi.onError(errorHandler);
    }
})

Output :

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

Reference :

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

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.