In modern Lightning Web Component (LWC) development, you often deal with components that live on the same page but don't have a direct parent-child relationship. So how do they talk to each other?
Thatβs where Lightning Message Service (LMS) comes in.
In this blog post, weβll walk through a practical example of using LMS to create a seamless communication channel between two separate components: a parent that sends messages, and a child that receives messages.
β What You'll Build
- A message channel to carry messages.
- A Parent component that publishes text input.
- A Child component that subscribes and displays the input in real-time.
π Step 1: Create the Message Channel
Create this file at:
πforce-app/main/default/messageChannels/TextMessageChannel.messageChannel-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
<masterLabel>TextMessageChannel</masterLabel>
<isExposed>true</isExposed>
<description>Message channel for sharing text between components</description>
<lightningMessageFields>
<fieldName>text</fieldName>
</lightningMessageFields>
</LightningMessageChannel>
πΉ Ensure the file name is TextMessageChannel.messageChannel-meta.xml
, and masterLabel
matches it exactly.
π¨βπ» Step 2: Parent Component (Publisher)
The ParentWire
component allows the user to input text and publishes it via the message channel.
πparentWire.html
<template>
<lightning-card title="Parent Component">
<lightning-input
label="Enter Text"
value={textInput}
onchange={handleChange}>
</lightning-input>
</lightning-card>
</template>
π¦ parentWire.js
import { LightningElement, wire } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import TEXT_MESSAGE_CHANNEL from '@salesforce/messageChannel/TextMessageChannel__c';
export default class ParentWire extends LightningElement {
textInput = '';
@wire(MessageContext)
messageContext;
handleChange(event) {
this.textInput = event.target.value;
publish(this.messageContext, TEXT_MESSAGE_CHANNEL, {
text: this.textInput
});
}
}
π§Ύ parentWire.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>63.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>
πΆ Step 3: Child Component (Subscriber)
The ChildWire
component listens for changes published through the message channel and displays the message.
π childWire.html
<template>
<lightning-card title="Child Component">
<p class="slds-p-around_medium">
Message from ParentWire: <strong>{receivedText}</strong>
</p>
</lightning-card>
</template>
π¦ childWire.js
import { LightningElement, wire } from 'lwc';
import { subscribe, MessageContext } from 'lightning/messageService';
import TEXT_MESSAGE_CHANNEL from '@salesforce/messageChannel/TextMessageChannel__c';
export default class ChildWire extends LightningElement {
receivedText = '';
@wire(MessageContext)
messageContext;
connectedCallback() {
subscribe(this.messageContext, TEXT_MESSAGE_CHANNEL, (message) => {
this.receivedText = message.text;
});
}
}
π§Ύ childWire.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>63.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>
π‘ How It Works
- User types a message into the input field in
ParentWire
. - The component publishes the message using LMS.
- The
ChildWire
component has already subscribed to this message channel. - The new message is received and rendered instantly.
β This setup is especially useful when your components are placed independently on a Lightning Page or Home Page and need to stay in sync.
β Use Cases for Lightning Message Service
- Real-time sync between unrelated components.
- Communication between Visualforce and LWC.
- App-wide broadcast messaging.
π Final Words
Lightning Message Service is a must-have tool when building scalable and modular applications in Salesforce. It simplifies inter-component communication without messy workarounds like custom events or global state.
π With LMS, your components can stay loosely coupled yet highly interactive.
Top comments (0)