0

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!

0

Yes, a component can be both subscriber and publisher. What's most likely going on is that you have a "race condition", where you're trying to send a message before the other end is ready to receive. To fix this, have each component send the other a message that they will respond to if loaded. This way, if A needs to send a message, it can check if it's already received the initial message from B, and if not, wait until it gets the message, and B can do the same. This will resolve any race conditions that may exist, as they will wait for both sides to be available.

1
  • Thanks. Let me explore that.
    – Pauline
    Jun 30 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.