Lightning Message Service (LMS) provides a standardized way to communicate between components across different programming models. It works across the DOM, allowing Lightning Web Components, Aura components, and Visualforce pages to exchange messages.
When to Use LMS
Use Lightning Message Service when you need to:
- Communicate between components that don’t have a parent-child relationship
- Send messages between LWC, Aura, and Visualforce
- Broadcast messages to multiple subscribers
- Decouple components from each other
For parent-child communication within LWC, use custom events and public properties instead of LMS.
Creating a Message Channel
Message channels are defined in XML metadata files in the messageChannels folder.
Record_Selected.messageChannel-meta.xml:
Key elements:
<masterLabel>: Display name for the channel
<isExposed>: Set to true to make available in Experience Builder sites
<lightningMessageFields>: Define the data fields in your message
Publishing Messages (LWC)
Components can publish messages to a channel that all subscribers will receive.
lmsPublisherWebComponent.js:
lmsPublisherWebComponent.html:
Key Publishing Concepts
- Import the channel: Use
@salesforce/messageChannel/ChannelName__c
- Wire MessageContext: Provides the context for publishing
- Create payload: Plain JavaScript object with your data
- Publish: Call
publish(context, channel, payload)
Subscribing to Messages (LWC)
Components can subscribe to a channel to receive messages.
lmsSubscriberWebComponent.js:
Key Subscribing Concepts
- Wire MessageContext: Required for subscribing
- Subscribe in connectedCallback: Ensure subscription is active when component loads
- Handle the message: Process the received payload
- Automatic unsubscribe: Using
@wire(MessageContext) handles unsubscribe automatically
If you subscribe using createMessageContext() instead of @wire, you must manually unsubscribe in disconnectedCallback().
Publishing from Aura Components
Aura components can also publish to message channels.
lmsPublisherAuraComponent.cmp:
lmsPublisherAuraComponentController.js:
Message Channel Scope
Lightning Message Service supports two scopes:
Application Scope
Default scope. Messages are delivered to subscribers in the same Lightning application.
Record Page Scope
Messages are delivered only to subscribers on the same record page.
Best Practices
Keep Payloads Simple
Send only the data needed. Often just an ID is sufficient:
Use Descriptive Channel Names
Name channels based on what they communicate, not which components use them:
Handle Subscription Lifecycle
Always ensure proper cleanup if not using @wire(MessageContext):
Test Message Handling
Mock the message service in your Jest tests:
Debugging LMS
Check Channel Configuration
- Verify the channel XML is deployed
- Ensure
<isExposed>true</isExposed> for Experience Cloud sites
- Check that field names match between publisher and subscriber
Verify Import Paths
Channel imports must match the file name:
Use Console Logging
Add logging to verify message flow: