1

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

2
  • Did you ever get this working? Facing the same issue myself
    – MikeO
    Commented Nov 3, 2021 at 8:31
  • No it didn't work for me, I went with platform Events. You can check this blog post by Santanu Boral on CDC and LWC. Commented Nov 3, 2021 at 13:14

1 Answer 1

0

I know this is coming bit late but you have to subscribe to /data/Custom_Object__ChangeEvent

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .