I have two (A and B)LWCs that do not have a parent child relationship. I am able to send messages from A to B. However, when I try to send the message in reverse after processing the results of the first message, the message does not seem to arrive on the other end.
I am wondering whether LWCs can be both publishers and subscribers.
Component A
subscribeToMessageChannel() {
this.subscription = subscribe(
this.messageContext,
RESET_MESSAGE,
(resetMsg) => this.processResetMessage(resetMsg)
);
}
processResetMessage(resetMsg){
console.log('received reset message'+ resetMsg.resetMsg);
}
----
if(this.deptValue.length > this.numOfDept){
this.depts = this.deptValue;
this.redDeptArr = this.deptValue;
this.searchArray.push(this.deptValue);
const payload = { searchString: this.searchArray};
publish(this.messageContext, SEARCH_MESSAGE, payload);
this.numOfDept = this.deptValue.length;
}
Component B component
subscribeToMessageChannel() {
this.subscription = subscribe(
this.messageContext,
SEARCH_MESSAGE,
(message) => this.handleMessage(message)
);
this.subscription = subscribe(
this.messageContext,
UNCHECKED_MESSAGE,
(msg) => this.handleUnchecked(msg)
);
this.subscription = subscribe(
this.messageContext,
SEARCH_TEXT,
(mess) => this.handleSearch(mess)
);
this.subscription = subscribe(
this.messageContext,
RADIO_MESSAGE,
(radmsg) => this.handleRadio(radmsg)
);
}
----
let reset = 'reset';
try{
const payload = { radioMsg: reset};
publish(this.messageContext, RESET_MESSAGE, payload);
console.log('published reset');
}
catch(error){
// console.log('Error in publishing ' + error);
// }
Anyone who has encountered such a scenario before?
Thanks!