I am using message channel to communicate between 2 lwc and both lwc must send and retrieve data. So i am passing value to subscriber and after that to publisher. How can i do it? Do i need to create another channel or can use same and how to wait until recieve message?
I tried and semss set value into attribute and trigger another message onchange is only way.
import { LightningElement, wire } from 'lwc';
import messageChannel from '@salesforce/messageChannel/Sample__c';
import {publish, MessageContext} from 'lightning/messageService'
export default class Publisher extends LightningElement {
@wire(MessageContext)
messageContext;
handleButtonClick() {
let message = {messageText: 'This is a test'};
publish(this.messageContext, messageChannel, message);
}
}
import { LightningElement, wire } from 'lwc';
import messageChannel from '@salesforce/messageChannel/Sample__c';
import { subscribe, MessageContext } from 'lightning/messageService';
export default class Subscriber extends LightningElement {
subscription = null;
@wire(MessageContext)
messageContext;
connectedCallback() {
this.handleSubscribe();
}
handleSubscribe() {
if (this.subscription) {
return;
}
this.subscription = subscribe(this.messageContext, messageChannel, (message) => {
console.log(message.messageText);
});
}
}