Platform Events Subscribe using Lightning Component

Platform events are part of Salesforce’s enterprise messaging platform. The platform provides an event-driven messaging architecture to enable apps to communicate inside and outside of Salesforce. Here let us discuss how to subscribe to the platform events in the lightning component using lightning: empApi. 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.we will be subscribing the platform event object as shown below.
Here is the simple lightning component that will subscribe to the platform events and listen for notifications.
<aura:component implements="flexipage:availableForAllPageTypes ,force:appHostable" access="global" >
    <lightning:empApi aura:id="empApi" />
    <aura:handler name="init" value="{!this}" action="{!c.onInit}"/>
    <aura:attribute name="subscription" type="Object" />
    <aura:attribute name="results" type="Object" />
    
    <lightning:input aura:id="channel" label="channel" name="channel" type="text" value="/event/Order_Fulfillment__e"/>
    <lightning:button label="Subscribe" onclick="{! c.subscribe }" />
    <lightning:button label="Unsubscribe" onclick="{! c.unsubscribe }" disabled="{!empty(v.subscription)}"/>
    
    {!v.results}
    
</aura:component>
({
    // Sets an empApi error handler on component initialization
    onInit : function(component, event, helper) {
        // Get the empApi component
        const empApi = component.find('empApi');
        //  below line to enable debug logging (optional)
           empApi.setDebugFlag(true);
    },
    // Invokes the subscribe method on the empApi component
    subscribe : function(component, event, helper) {
        // Get the empApi component
        const empApi = component.find('empApi');
        // Get the channel from the input box
        const channel = component.find('channel').get('v.value');
        // Replay option to get new events
        const replayId = -1;

        // Subscribe to an event
        empApi.subscribe(channel, replayId, $A.getCallback(eventReceived => {
            // Process event (this is called each time we receive an event)
			component.set('v.results', JSON.stringify(eventReceived.data.payload));
            console.log('Received event 1 ', eventReceived.data);

            console.log('Received event2  ', eventReceived.data.payload);
        }))
        .then(subscription => {
            // Confirm that we have subscribed to the event channel.
            // We haven't received an event yet.
            console.log('Subscribed to channel ', subscription.channel);
            // Save subscription to unsubscribe later
            component.set('v.subscription', subscription);
        });
    },

    // Invokes the unsubscribe method on the empApi component
    unsubscribe : function(component, event, helper) {
        // Get the empApi component
        const empApi = component.find('empApi');
        // Get the subscription that we saved when subscribing
        const subscription = component.get('v.subscription');

        // Unsubscribe from event
        empApi.unsubscribe(subscription, $A.getCallback(unsubscribed => {
          // Confirm that we have unsubscribed from the event channel
          console.log('Unsubscribed from channel '+ unsubscribed.subscription);
          component.set('v.subscription', null);
        }));
    }
})

 

once a platform event is published, you can able to the response from the subscribed lightning component as shown below