Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
DictionaryMMessage Channel
DevelopmentIntermediate

Message Channel

A Message Channel in Salesforce is a typed pub-sub conduit used by Lightning Web Components (LWC) and Aura Components to communicate with each other across the same Lightning page, even when the components are nested in different DOM trees or do not have a parent-child relationship.

§ 01

Definition

A Message Channel in Salesforce is a typed pub-sub conduit used by Lightning Web Components (LWC) and Aura Components to communicate with each other across the same Lightning page, even when the components are nested in different DOM trees or do not have a parent-child relationship. Message Channels are part of the Lightning Message Service (LMS) framework introduced in Spring 20; each channel is a metadata record declaring the channel name and the schema of messages that pass through it. Components publish messages on the channel; subscribed components receive them.

Lightning Message Service solves a real architectural problem. Lightning App Builder pages combine many components from different namespaces (standard, custom, AppExchange), and these components frequently need to coordinate (selecting an item in one component should filter another). Without Message Channels, the coordination required either parent-child events (only works when components share a parent) or custom JavaScript globals (fragile and namespace-unsafe). Message Channels provide a clean, declarative, cross-component-tree communication mechanism with type safety.

§ 02

How Lightning Message Service enables cross-component communication

The Message Channel metadata type

A Message Channel is defined as a .messageChannel-meta.xml file in the SFDX source tree. The file declares the channel''s API name, label, isExposed flag (whether other namespaces can use it), and the message fields (typed properties: string, integer, boolean). Once deployed, the channel becomes available for components to publish or subscribe to.

Publishing and subscribing in LWC

LWC components publish messages with the publish function from the @salesforce/messageChannel/MyChannel__c module. Subscribing components import subscribe and call it with the channel reference and a handler function. The framework delivers published messages to every subscribed component on the same page, regardless of where each component sits in the DOM tree.

Scope and lifetime

Message Channels are scoped to a single Lightning App page, Lightning Page, or Lightning App. Messages do not cross page boundaries; navigating to a new record clears the message subscriptions and they re-establish on the new page. The lifetime model prevents components on different pages from accidentally interfering with each other.

Type safety via channel fields

The Message Channel declaration includes typed message fields. Publishers must include the declared fields in the message payload; subscribers receive the typed values. The type safety is the main improvement over older browser-event-based communication, which was string-based and namespace-unsafe.

Aura compatibility

Lightning Message Service is cross-framework: Aura Components and LWC can publish and subscribe to the same channels. This is critical for orgs with mixed Aura and LWC codebases; one channel coordinates components across both frameworks. The framework abstracts the differences in API; the behavior is consistent.

Visualforce interop

LMS extends to Visualforce pages that embed Lightning components. A Visualforce page with the apex:slds tag and embedded LWC can publish or subscribe to Message Channels the same way a pure LWC component can. This is useful when migrating from Visualforce to Lightning gradually; the channels coordinate components across the two surfaces.

Performance and limits

Message Channels deliver messages synchronously to every subscribed component on the page. High publish frequency (many messages per second) can cause UI jank if subscribers are heavy. Design publishers to throttle or debounce when the upstream source produces fast updates. The framework does not enforce limits; it''s a design discipline.

§ 03

Build a Message Channel and use it from LWC

The end-to-end pattern is: define the channel, deploy the metadata, publish from one component, subscribe in another.

  1. Define the Message Channel

    Create force-app/main/default/messageChannels/MyChannel.messageChannel-meta.xml with the lightningMessageChannel type, masterLabel, isExposed, and lightningMessageFields.

  2. Deploy the channel

    Use the Salesforce CLI: sf project deploy start to push the channel definition to the org.

  3. Publish from a component

    In the publisher LWC: import {publish, MessageContext} from ''lightning/messageService''; import MY_CHANNEL from ''@salesforce/messageChannel/MyChannel__c''. Call publish(this.messageContext, MY_CHANNEL, payload).

  4. Subscribe in another component

    In the subscriber LWC: import {subscribe, MessageContext} from ''lightning/messageService''. Call subscribe(this.messageContext, MY_CHANNEL, handlerFn) in connectedCallback.

  5. Unsubscribe in disconnectedCallback

    Clean up the subscription in disconnectedCallback to prevent memory leaks: unsubscribe(this.subscription).

  6. Test on a Lightning Page

    Place both components on the same Lightning Page via App Builder. Verify the publisher and subscriber communicate as expected.

Mandatory fields
Message Channel definitionrequired

The XML metadata declaring the channel.

Publisher componentrequired

The LWC that calls publish.

Subscriber componentrequired

The LWC that calls subscribe and handles messages.

Message Contextrequired

The injected @wire context required for both publish and subscribe.

Subscription cleanuprequired

Required in disconnectedCallback to prevent leaks.

Gotchas
  • Subscriptions must be cleaned up in disconnectedCallback. Without cleanup, the components leak references and accumulate handlers.
  • Messages do not cross page boundaries. Navigating to a new record breaks the subscriptions; design accordingly.
  • The isExposed flag controls cross-namespace visibility. Set to true to allow other packages to publish or subscribe to the channel.
  • Performance can degrade with high message frequency. Throttle or debounce publishers when upstream sources produce fast updates.
§

Trust & references

Sources

Cross-checked against the following references.

Official documentation

Straight from the source - Salesforce's reference material on Message Channel.

Was this entry helpful?
Help us write better definitions. Quick reactions or detailed edit suggestions.

About the Author

Dipojjal Chakrabarti is a B2C Solution Architect with 29 Salesforce certifications and over 13 years in the Salesforce ecosystem. He runs salesforcedictionary.com to help admins, developers, architects, and cert/interview candidates sharpen their fundamentals. More about Dipojjal.

§

Test your knowledge

Q1. What is a Message Channel?

Q2. What problem do Message Channels solve?

Q3. When should you use parent-child patterns instead?

§

Discussion

Loading…

Loading discussion…