2

I am using the empAPI module in LWC and 99% of the code is working. The subscription, receiving of events, etc. all work -- except the unsubscribe method.

It is very important for me to unsubscribe from the channel in my component. When I call the method, I don't get anything back and it does not execute the code in the unsubscribe block. I do get the console.log directly after the handleUnsubscribe though so I know it is entering that.

I have chipped away at my code to see if it was something else and I have gotten it down to this point. It is still not working as expected:

this.isSubscribed = false;
this.subscription = {};

  // Handles subscribe button click
  handleSubscribe() {
    console.log('Subscribing to channel: ', this.subscription);
    if (!this.isSubscribed) {
        const messageCallback = (response) => {
            console.log('New message received: ', JSON.stringify(response));
            this.handleUnsubscribe();
        };  



        subscribe(this.channelName, -1, messageCallback).then(response => {
            console.log('Successfully subscribed to : ', JSON.stringify(response.channel));
            this.isSubscribed = true;
            this.subscription = response;
        });
    }
}

handleUnsubscribe() {
    console.log('Unsubscribing from channel: ', this.subscription);
    // Invoke unsubscribe method of empApi
    unsubscribe(this.subscription, response => {
        console.log('unsubscribe() response: ', JSON.stringify(response));
        // Response is true for successful unsubscribe
        this.isSubscribed = false;
    });
}

1 Answer 1

3

I found the issue. We are an ISV and this is part of a managed package with a namespace.

The subscribe method worked with the following event name format:

*namespace*__Event__e

The unsubscribe method did not like the namespace, however. It was frustrating that unsubscribe didn't return anything (not even an error). It simply did not work.

I tested the same code with the following event name format:

Event__e

And both subscribe and unsubscribe work so I am marking this as resolved.

Outside the scope of this question: I need to test to see how a subscriber org handles managed package event subscription. I assume (and hope) we need the namespace because otherwise conflict issues could arise.

Might be a Salesforce bug? I am going to raise to my contacts there and will comment on this post when I learn more.

3
  • The namespace should be implicit in all your in-package code, so you should not need it. As to clashes, if the code trying to use your channel is subscriber code, they must use your namespace prefix.
    – Phil W
    Aug 19, 2023 at 12:46
  • Thanks Phil, I stepped away from this, came back a few hours later and now both subscribe and unsubscribe are working with the namespace prefix. Not sure what happened but I am hoping it was transient.
    – Rory
    Aug 21, 2023 at 14:59
  • 1
    Glad you're sorted, but always worrying when there's no chance of a root cause analysis... I do stand by my comment, above, though would say that some metadata types (such as packaged flows) do have to include namespace prefixes...
    – Phil W
    Aug 21, 2023 at 16:41

You must log in to answer this question.

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