0

I am following lightning/empApi example to subscribe to a Platform Event (PE) channel. I am able to successfully subscribe to one channel, but I cannot subscribe to multiple channels at once. I am using LWC and not any aura component.

Questions

  1. How can I subscribe to multiple channels at once?
  2. What would be the proper way to subscribe using empApi?

Example code

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

export default class EmpApiLWC extends LightningElement {
    channelName = ['/event/Test__e', '/event/AnotherPE__e'];
    isSubscribeDisabled = false;
    isUnsubscribeDisabled = !this.isSubscribeDisabled;

    subscription = {};
    // Handles subscribe button click
    handleSubscribe() {
        // Callback invoked whenever a new event message is received
        const messageCallback = function (response) {
            console.log('New message received: ', JSON.stringify(response));
            // Response contains the payload of the new message received
        };

        // Invoke subscribe method of empApi. Pass reference to messageCallback
        subscribe(this.channelName, -1, messageCallback).then((response) => {
            // Response contains the subscription information on subscribe call
            console.log(
                'Subscription request sent to: ',
                JSON.stringify(response.channel)
            );
            this.subscription = response;
            this.toggleSubscribeButton(true);
        });
    }
}

Error message

"400::The channel specified is not valid {/event/Test__e,/event/AnotherPE__e}"

What I tried, but didn't work

subscribe(JSON.stringify(this.channelName), -1, messageCallback).then((response) => {...}
7
  • @identigral no -- it refers to links that have moved and I am not using cometD. I am not using Aura component, strictly LWC. Any thoughts on how else to properly subscribe to multiple channels using LWC and empApi? Commented Feb 2, 2023 at 22:04
  • 1
    I think it fairly clear you need to iterate through the channel names and subscribe to each in turn. The documentation clearly states that the channel given to subscribe is a string, not a string array. Each subscription will need to be stored in the component separately too, but arrays and JavaScript object properties are good for that sort of thing.
    – Phil W
    Commented Feb 2, 2023 at 22:08
  • 1
    Sounds right to me. Just make sure to store the subscriptions appropriately, not just the last one received.
    – Phil W
    Commented Feb 2, 2023 at 22:42
  • 1
    Glad it worked for you and, more specifically, that you worked it out yourself from some hints. Always best because you learn more deeply.
    – Phil W
    Commented Feb 2, 2023 at 23:18
  • 1
    BTW you could use this.channelName.forEach(...) to do the iteration.
    – Phil W
    Commented Feb 2, 2023 at 23:19

1 Answer 1

0

Thanks to @PhilW as I was able to find a solution. I have to iterate through all possible channels to subscribe to and then subscribe in a loop.

for(let i = 0; i < this.channelName.length; i++) {
    subscribe(this.channelName[i], -1, messageCallback).then((response) => {...}
}

You must log in to answer this question.

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