Sub topic 3
Managing LMS Subscriptions Across Salesforce Components
Assume you know LWC classes, lifecycle hooks, @wire, and Salesforce metadata deployment. A subscription is a listener that receives messages published on a message channel.
Manage the subscription lifecycle
Subscribe in connectedCallback() when the component enters the DOM. Store the returned subscription so you can remove it in disconnectedCallback(). This prevents duplicate listeners when a component is rendered again and avoids handling messages after removal.
import { LightningElement, wire } from 'lwc';
import {
subscribe,
unsubscribe,
APPLICATION_SCOPE,
MessageContext
} from 'lightning/messageService';
import RECORD_SELECTED from '@salesforce/messageChannel/RecordSelected__c';
export default class RecordDetail extends LightningElement {
subscription = null;
selectedId;
@wire(MessageContext)
messageContext;
connectedCallback() {
if (!this.subscription) {
this.subscription = subscribe(
this.messageContext,
RECORD_SELECTED,
message => {
this.selectedId = message.recordId;
},
{ scope: APPLICATION_SCOPE }
);
}
}
disconnectedCallback() {
if (this.subscription) {
unsubscribe(this.subscription);
this.subscription = null;
}
}
}Choose the message scope
The default scope limits delivery to the active area. Use it when publishers and subscribers belong to the same workspace or page context. APPLICATION_SCOPE allows subscribers anywhere in the Lightning application to receive the message.
| Scope | Use it when |
|---|---|
| Default | Communication should stay within the active area |
| `APPLICATION_SCOPE` | Any supported component in the application may respond |
Design small payloads
Treat the payload as a stable contract. Send serializable values such as record IDs, operation names, and small arrays. Avoid passing component instances, DOM nodes, or large records. A subscriber should receive enough information to act, then load current data itself.
Bridge LWC, Aura, and Visualforce
LMS can connect supported LWC, Aura, and Visualforce components through the same channel metadata. Aura uses lightning:messageChannel and reads fields from the received message. Visualforce uses the LMS APIs available through sforce.one. Keep field names and types consistent across all three technologies.
Choose the right communication pattern
Use LMS when components have no useful parent-child relationship or cross framework boundaries. Use public properties and custom events for direct parent-child communication. Use shared state or server-side data when many components need durable, synchronized application data.
Test and debug
Test that the publisher sends the expected channel and payload, the subscriber updates state, and disconnectedCallback() removes the subscription. Log the payload temporarily, verify the channel metadata is deployed, and check that the scope matches the components' locations.
Exercise
Why should a subscriber call unsubscribe in disconnectedCallback()?
Show answer
To prevent stale or duplicate listeners from receiving messages after the component is removed.