Sub topic 3
Message channels as the shared contract
This chapter assumes you know basic Lightning Web Components (templates, JavaScript classes, @wire), the connectedCallback and disconnectedCallback hooks, and how a Salesforce DX project is laid out. It covers what a message channel is and why every publisher and subscriber depends on it. Creating and deploying the channel file is covered in the next chapter, so the XML here is only for reading.
What a message channel is
Lightning Message Service (LMS) connects components that have no parent-child relationship. Those components cannot call each other, and they share no DOM tree to bubble events through. They need something else both sides already agree on.
That agreement is the message channel. A message channel is a metadata component that names a communication path and describes the fields a message on that path carries. Publishers send to the channel and subscribers listen on it. Neither side knows the other exists.
Think of a radio frequency. A broadcaster and a listener never meet. They only need to be tuned to the same frequency and to agree on what the broadcast contains.
What the channel defines
A channel definition holds three kinds of information:
- A name. This is the
fqn(fully qualified name) of the channel, for exampleRecordSelected. - Labels and descriptions. These help people find and understand the channel in Setup and in code.
- Message fields. Each field has a name and a description. Together they document the shape of the payload.
Here is the channel definition used in this tutorial:
<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata" fqn="RecordSelected">
<description>Published when a user selects a record in a list.</description>
<isExposed>true</isExposed>
<lightningMessageFields>
<description>Id of the selected record</description>
<fieldName>recordId</fieldName>
</lightningMessageFields>
<masterLabel>Record Selected</masterLabel>
</LightningMessageChannel>The file lives at force-app/main/default/messageChannels/RecordSelected.messageChannel-meta.xml. The isExposed element controls whether the channel is available to components outside your namespace.
How components refer to the channel
Code never reads the XML. Components import the channel by name, and the platform adds the __c suffix, as it does for custom objects and fields.
import RECORD_SELECTED from '@salesforce/messageChannel/RecordSelected__c';Publisher and subscriber both use this import. The imported value identifies the channel at runtime. This is why the channel is called a contract: if either side uses a different channel, the message is never delivered.
The Aura syntax uses the same name through the type attribute of lightning:messageChannel, as in type="RecordSelected__c".
What the channel does not enforce
The field list is documentation, not validation. LMS does not check that a published payload matches the declared fields. If a publisher sends { id: '001...' } instead of { recordId: '001...' }, the message still arrives and the subscriber reads undefined for recordId.
Common errors
- Importing
RecordSelectedwithout the__csuffix. The import fails to resolve. - Importing the channel before it is deployed to the org. The component cannot be saved or deployed until the channel exists.
- Different field names in publisher code and channel definition. Nothing fails loudly. The subscriber just gets
undefined. - Creating one broad channel for everything. Subscribers then receive messages they must filter out. Prefer one channel per kind of event.
Exercise
A publisher sends { id: '001xx' } on the RecordSelected channel, which declares only a recordId field. The subscriber reads message.recordId. What happens, and why?
Show answer
The message is delivered, but the subscriber reads undefined for recordId. LMS does not validate payloads against the channel's declared fields. The field list only documents the contract, so the publisher and subscriber code must be kept consistent by the developers.