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
Precisely explained Manish.
ReplyDeleteThank you !
Thank you Manjiri !
DeleteNicely explained keep it up Manish.
ReplyDeleteThank you Rahul !
Delete