Sub topic 2
What Lightning Message Service Is and Where It Works
Lightning Message Service (LMS) lets components that have no parent-child relationship exchange messages. This chapter covers what LMS is, which technologies can use it, and how a message travels. It assumes you know basic Lightning Web Components (LWC), including templates, JavaScript classes, and the @wire decorator. It also assumes you know how Lightning pages place components in regions.
The idea in one paragraph
LMS is a publish-subscribe system built into Lightning Experience. One component publishes a message to a named channel. Any component that subscribed to that channel receives the message. The publisher does not know who is listening, and the subscribers do not know who sent the message. The only thing they share is the channel.
Where LMS works
LMS connects three kinds of UI code on the same page:
- Lightning web components
- Aura components
- Visualforce pages, when they are displayed inside Lightning Experience
That makes LMS the standard way to link a modern LWC to older Aura or Visualforce code during a gradual migration. The three technologies use different syntax, but they all talk to the same channel.
LMS has limits you should know before you design around it:
- It works inside Lightning Experience, including Lightning pages, the utility bar, and Lightning console apps.
- It is not available on every surface. For example, it does not reach components in an external website or a different browser tab.
- It carries messages only while the page is open. Nothing is stored. A component that subscribes after a message was published does not receive it.
Check the current Salesforce documentation for the exact list of supported containers, since support differs for Experience Cloud sites and the Salesforce mobile app.
How a message travels
The diagram shows the three parts: a publisher, a channel, and subscribers in different technologies.
The delivery has these steps:
- A subscriber registers interest in a channel when it loads.
- A publisher sends a payload, a plain object of data, to the same channel.
- LMS calls the handler of every active subscriber with that payload.
- When a subscriber is removed from the page, it must unsubscribe so it stops receiving messages.
Later chapters cover each step in code. For now, notice that the channel is the contract between components.
The pieces you will use
| Piece | What it is | Where it appears |
|---|---|---|
| Message channel | A metadata file that names the channel and lists its fields | Deployed to the org |
| `MessageContext` | An object that tells LMS where a component sits on the page | Wired in LWC |
| `publish` | Sends a payload to a channel | `lightning/messageService` |
| `subscribe` | Registers a handler for a channel | `lightning/messageService` |
| `unsubscribe` | Removes a handler | `lightning/messageService` |
An LWC imports the service functions like this:
import {
publish,
subscribe,
unsubscribe,
APPLICATION_SCOPE,
MessageContext
} from 'lightning/messageService';You will not use all of these in every component. A publisher needs publish and MessageContext. A subscriber needs subscribe, unsubscribe, and MessageContext, plus APPLICATION_SCOPE if it must hear messages from outside its own area of the page.
Common mistakes at this stage
- Expecting delivery to late subscribers. LMS does not replay old messages. If the subscriber loads after the publish call, it misses the message. Subscribe in
connectedCallbackso the component listens as early as possible. - Treating LMS as shared state. It passes events, not stored values. If components need the current value on load, they must read it from another source, such as a record or an Apex call.
- Assuming it works across all containers. Test on the real page type you will ship on.
Exercise
A Visualforce page and an LWC sit on the same Lightning Experience record page. The LWC publishes a message, but the Visualforce page loads two seconds later. Does the Visualforce page receive the message, and why?
Show answer
No. LMS does not store or replay messages. Only subscribers that are active at the moment of publishing receive the payload. The page needs another way to get the current value, such as reading the record directly.