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
- How can I subscribe to multiple channels at once?
- 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) => {...}
cometD
. I am not usingAura
component, strictlyLWC
. Any thoughts on how else to properly subscribe to multiple channels usingLWC
andempApi
?this.channelName.forEach(...)
to do the iteration.