Sub topic 12
When to Use LMS and When to Use Something Else
This chapter assumes you can build Lightning web components, know the publish and subscribe calls from earlier chapters, and understand message channels and scope. It helps you decide whether LMS is the right tool for a given communication need.
What LMS is good at
LMS fits when two or more components must exchange information and have no direct parent-child relationship. The components can be LWC, Aura, or Visualforce, and they can sit in different regions of a Lightning page. The sender does not need to know who listens, and listeners do not need to know who sends.
That flexibility has a cost. A message channel is metadata you must deploy. Every subscriber needs a MessageContext, a subscription, and an unsubscribe step. Messages are not typed, and nothing in the platform tells you who is listening. If a simpler pattern works, use it.
The alternatives
Salesforce gives you several ways to move data between components. Each fits a different component relationship.
| Situation | Pattern | Why |
|---|---|---|
| Parent passes data down to a child | Public property with `@api` | Direct, reactive, and needs no extra metadata |
| Child tells its parent something happened | `CustomEvent` dispatched from the child | Follows the DOM event model, and the parent handles it in its template |
| Parent calls a child's behavior | Public `@api` method | Explicit, and limited to the owning parent |
| Components far apart in the same DOM tree | `CustomEvent` with `bubbles` and `composed` | Works without metadata, but couples components to the page structure |
| Unrelated components on one page, including Aura and Visualforce | LMS | Decoupled, and works across frameworks |
| Components that need the same server data | `@wire` with Apex or Lightning Data Service | Shared cache keeps record data consistent |
| Updates from the server or other users | Platform events with `lightning/empApi` | Delivers events across sessions, not just within one page |
A decision flow
Ask these questions in order. Stop at the first match.
Common mistakes
Using LMS between a parent and child is the most common one. The parent can pass data with @api and receive events with CustomEvent. LMS in that case hides the data flow and adds a deployment dependency.
A second mistake is treating LMS as shared state. A message is a one-time signal. A component that subscribes after the message was published does not receive it. If a component needs the current value when it loads, read it from a source of truth such as a record, a wire adapter, or a service module, and use LMS only to announce changes.
A third mistake is expecting LMS to work outside its boundary. Messages stay within the current Lightning Experience page or application, depending on scope. They do not reach other browser tabs or other users. For those cases, use platform events.
A fourth mistake is using LMS to send large or complex data. Keep the payload small, such as an Id, and let each subscriber fetch what it needs. The payload chapter covers this.
Trade-offs to keep in mind
- Coupling: LMS removes coupling between components, but both sides are still coupled to the channel and its field names. Changing a field name breaks every consumer.
- Traceability: With a custom event, you can follow the handler in the parent template. With LMS, you must search the code base for the channel name.
- Cleanup: Every subscription needs an unsubscribe in
disconnectedCallback. Events and public properties need no such cleanup. - Testability: LMS functions need mocking in Jest, while events and
@apineed only the component itself.
Exercise
A record list component and a detail panel sit in separate regions of a Lightning page, with no common component parent. The detail panel should show the record the user clicks. Separately, a banner must warn the user when another user edits that same record in a different browser. Which pattern fits each need, and why?
Show answer
Use LMS for the list-to-detail communication. The components are unrelated, both live on the same page, and the signal comes from a user action in the page. Use a platform event, received with lightning/empApi, for the banner. The edit happens in another user's session, and LMS messages never leave the current page.