Dear #Trailblazers,
Welcome back 🙂 to new blog post. In this blog post we are going to talk about Publish Subscribe model in Lightning Web component. Before We start let’s learn about what Pub ( Publish ) – Sub ( Subscribe ) model is.
Pub-Sub Model
Is used to transfer the information between the component which are not in the same DOM hierarchy what does this meant that the components are not connected/related with each other using parent-child relationship.
Below is the simple diagram to illustrate the Pub-Sub.
In the above image, one component is acting as the publisher component and there are three different components acting as subscriber components. Between the Publisher & Subscribers component, there is a change and this channel is known as the Pub-Sub model.
Key Points to Remember
- Pub-Sub is nothing it is a JavaScript file containing multiple methods
- Pub-Sub is like an Application event in Aura Component
Now, Let’s start with the implementation part
Use Case
We are going to take a very simple example, There are two components and both components do not have any relation. As a Developer, we need to make them talk & transfer the data from one component to another component.
P.S. You can have as many components as per business requirements.
Step1 – Create a PubSub Lightning Component and use the below JS code for the same.
Step2 – Create a Publisher Component named “pubsubPublisher” and use the below code for both JS and HTML file
Code Explanation
import pubsub from ‘c/pubsub’; in this line, we are importing the pubsub component into our publisher component
pubsub.fire(‘simplevt‘, message ); This line executes the event names simplevt so any component which has subscribed simplevt event will be able to get the message.
Step3 – Create a Subscriber Component named “pubsubSubscriber” and use code from the below gist file for all three ( template, JS & XML file )
Code Explanation
#1 import pubsub from “c/pubsub”; We are importing the pubsub file so that we can access all the methods that we created in pubsub file and export those functions.
#2 As this is a Subscriber Component, so we are registering the event
pubsub.register(“simplevt”, this.handleEvent.bind(this));
#3 This subscriber component must have a JS function that will execute when the component receives an event that we have created below method
handleEvent(messageFromEvt) {
window.console.log("event handled ", messageFromEvt);
this.message = messageFromEvt
? JSON.stringify(messageFromEvt, null, "\t")
: "no message payload";
}
Step4 – Test the Publish-Subscribe Component
As we have developed the required Component, we can add both the component either inside any Record Page, Home Page or App Page and do the Testing.
Check out the YouTube video with the explanation of each and every single detail.
Keep Learning. Sharing is Caring 🙂 #KeepBlazing #LearnShareRepeat #AskPanther #SfdcPanther
Please do provide your valuable feedback to use. You can fill in the feedback Here.
Resource:-
Can we publish the one message to two different lwc component which are not apart of same record details page ?
My mean is one Pub component(on account details page) and two sub component . sub1 is on Account detail page and sub2 is on Contact detail page. If I click on Button of Pub comp which is on Account page, will it also publish the message to sub2 comp as well ?
I believe yes you can. You can try it once.
We cannot use pubsub in 2 diff pages … it should be on single page only..
Page Reference should be same to handler pub-Sub model .
In your case you can use LMS to handler your requiremnet when the publisher and subscriber are on different pages .
I saw so many videos and blogs about this pubsub .. but this is so clear.. Thank you so much
Please advice if this code can be used in communities as well
I believe yes. You can give it a try
God damn too many ads, man!!
I have updated the settings and new setting will take place in couple of days
How can I use the unregiter method please?
I would suggest use Lightning Message Service – https://pantherschools.com/lightning-message-services/
Good and helpful article, thanks. There is a small typo in ‘register’. Look for ‘regiser’ on the page and you’ll find it.
Thanks! Made the required changes.
Great Article! It works well. Thank you.
Many people are suggesting to go with Lightning Message Service but I don’t see much article around it. I tried one of the article but it did not work as per what they have explained.