Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Full Message Channel entry
How-to guide

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.

By Dipojjal Chakrabarti · Founder & Editor, Salesforce DictionaryLast updated May 21, 2026

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.

See the full Message Channel entry

Message Channel includes the definition, worked example, deep dive, related terms, and a quiz.