Salesforce Lightning empAPI Module - Test Platform, CDC Events

 


Hello Trailblazers,

In this blog I have explained use of EMP API which is used to subscribe for events. You can create Lightning Component which uses this EMP API for subscribing to various streaming channels such as Platform Events, Change Data Capture Events, Generic Events.

We can use this implementation for testing Platform Events or Change Data Capture events in local environment.

Lightning/empApi module has below methods which are used for implementing events subscription.

 Method Name    Parameter Name  Description
 subscribe    Subscribes to the streaming channel, this gives subscription object which we use to unsubscribe later.
   onMessageCallback  Callback function which is invoked whenever an event is received
   replayId  This number type parameter defines what point in the stream to replay events from
   channel  This defines channel name to which we want to subscribe. 
 unsubscribe   This method is used to unsubscribe from particular channel with the help of subscription object.
   subscription Object returned by subscribe method.
   callback Callback function 
 onError    Registers a listener to errors that the server returns.
   callback  Callback function which is invoked in case of Error.
 setDebugFlag    Set to true or false to turn console logging on or off respectively.
   flag  Set to true or false to turn console logging on or off respectively.
 isEmpEnabled    Returns a promise that holds a Boolean value. The value is true if the EmpJs Streaming API library can be used in this context; otherwise false.

 Let’s see with demo.Create new Lightning Web Component and copy below code.

empAPIDemo.html  


In above code we will be entering channel name as an input. After entering channel name we will be clicking Subscribe button and the event listening will be initiated. Similarly Unsubscribe button will stop listening for event on specified channel.

For listening Platform Events channel name will be /event/<PLATFORM_EVENT_NAME>

For listening Change Data Capture Events channel name will be /data/<STANDARD_OBJECT_NAME>ChangeEvent

/data/<CUSTOM_OBJECT_NAME>__ChangeEvent



empAPIDemo.js 


Before using methods described above you need to import those using 

import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';

messageCallback function will be called every time whenever event is received. Response will hold details regarding received event. Sample response would be like

{"data":{"schema":"NcxzKJd4Tl_6kTFC7d9-DQ","payload":{"LastModifiedDate":"2022-03-28T11:20:07Z","Status":"Escalated","ChangeEventHeader":{"commitNumber":11307037405766,"commitUser":"0052800000271jWAAQ","sequenceNumber":1,"entityName":"Case","changeType":"UPDATE","changedFields":["Status","LastModifiedDate"],"changeOrigin":"com/salesforce/api/soap/54.0;client=SfdcInternalAPI/","transactionKey":"00052b3e-af08-6582-4729-8323d17a97e6","commitTimestamp":1648466407000,"recordIds":["50028000008hPQiAAM"]}},"event":{"replayId":5147867}},"channel":"/data/CaseChangeEvent"}

You can use lightning:empApi as well within aura component to implement above.

Let me know if you have any querries/suggessions. Thank you.

Comments

Post a Comment

Popular posts from this blog

Salesforce Flow - Create multiple records from user input

Scan barcodes from Salesforce Mobile App

Zip or Unzip Files in Salesforce