I have enabled the Change Data Capture for my Custom_Object__c
and I am trying to subscribe to these events from a Lightning web component using EmpApi
of the LWC framework.
Here is my code.
// subscribe to Change Data Capture(CDC) for Custom object
import {
subscribe as subscribeEmpApi,
unsubscribe as unsubscribeEmpApi,
onError /* ,
setDebugFlag,
isEmpEnabled */
} from "lightning/empApi";
export default class MyLwc extends NavigationMixin(LightningElement) {
// CDC
cdcChannelName = "/data/ChangeEvents";
cdcSubscription = {};
// ...
connectedCallback() {
// this.subscribeToMessageChannel();
this.registerCDCErrorListener();
this.handleCDCSubscribe();
}
disconnectedCallback() {
this.handleCDCUnsubscribe();
}
/* cdc start */
handleCDCSubscribe() {
// Callback invoked whenever a new event message is received
const messageCallback = function (response) {
console.log("CDC New message received: ", JSON.stringify(response));
// Response contains the payload of the new message received
};
// Invoke subscribe method of empApi. Pass reference to messageCallback
subscribeEmpApi(this.cdcChannelName, -1, messageCallback).then(
(response) => {
// Response contains the subscription information on subscribe call
console.log(
"CDC Subscription request sent to: ",
JSON.stringify(response.channel)
);
this.cdcSubscription = response;
}
);
}
registerCDCErrorListener() {
onError((error) => {
console.log(
"CDC Received error from server: ",
JSON.stringify(error)
);
});
}
handleCDCUnsubscribe() {
unsubscribeEmpApi(this.cdcSubscription, (response) => {
console.log(
"unsubscribeEmpApi() response: ",
JSON.stringify(response)
);
});
}
/* cdc end */
}
I am getting the below console log as the subscription is requested successfully.
CDC Subscription request sent to: "/data/ChangeEvents"
But no message received when I edited the record from UI. Am I missing something here? Is it not supported for LWC?
Note: I have given View All permission to the Custom Object on my profile and I am not running into any error