Platform Events in LWC Salesforce

by Rijwan Mohmmed

Hello friends, today we will discuss How to use Platform Events in LWC Salesforce. Here we will publish the events in Apex and Subscribe to the LWC component.

Also, check this: Use Dynamic Styling In LWC Salesforce


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 LWC.
  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 LWC 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 LWC component for Subscribe the event and show data which send by the platform event.

lWCPlatformEvent.HTML :

    <lightning-card title="Platform Event In LWC Techdicer" icon-name="standard:contact">
        <div class="slds-m-around_medium">
            <p>Status : {status}</p>
            <p>Message : {message}</p>
            <p>Record Id : {recordId}</p>

lWCPlatformEvent.JS :

import { LightningElement, api, track } from 'lwc';
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

export default class LWCPlatformEvent extends LightningElement {
    @track status;
    @track message;
    @track recordId;

    subscription = {};
    @api channelName = '/event/Techdicer_Event__e';

    connectedCallback() {
        // Register error listener     

    // Handles subscribe button click
    handleSubscribe() {
        // Callback invoked whenever a new event message is received
        const self = this;
        const messageCallback = function (response) {
            console.log('New message received 1: ', JSON.stringify(response));
            console.log('New message received 2: ', response);
            var obj = JSON.parse(JSON.stringify(response));
            let objData =;
            self.message = objData.message__c;
            self.status = objData.Status__c;
            self.recordId = objData.recordId__c;
            self.ShowToast('Techdicer Plaform Event', self.message, 'success', 'dismissable');

        // Invoke subscribe method of empApi. Pass reference to messageCallback
        subscribe(this.channelName, -1, messageCallback).then(response => {
            // Response contains the subscription information on subscribe call
            console.log('Subscription request sent to: ', JSON.stringify(;
            this.subscription = response;

    //handle Error
    registerErrorListener() {
        onError(error => {
            console.log('Received error from server: ', JSON.stringify(error));

    ShowToast(title, message, variant, mode) {
        const evt = new ShowToastEvent({
            title: title,
            message: message,
            variant: variant,
            mode: mode

lWCPlatformEvent.js-meta.xml :

<?xml version="1.0"?>
<LightningComponentBundle xmlns="">
        <targetConfig targets="lightning__HomePage">
            <property name="channelName" type="String" />

Output :


Reference :

  1. Platform Events In LWC

You may also like


Swapnil Jain March 15, 2023 - 7:24 am

Hi Everyone,
This subscription of platform event in LWC does not for Communities, so if you component is used in Community than any streaming Api won’t work in that, so you need to use CometD library to implement and subscribe the platform event in LWC in community.

Rick Baker March 23, 2023 - 2:09 pm

Beautifully done. Thanks so much for this. Perfect example. Just what I was look for.

Pranav May 25, 2023 - 8:29 pm

I have to show modal popup when user mark the opportunity as closed won using opportunity stages path. can we implement this using platform event.

Rijwan Mohmmed May 26, 2023 - 2:57 am

Yupp We Can


Leave a Comment