Sub topic 4
Creating and deploying a message channel
This chapter assumes you know basic Lightning Web Components, can work in a Salesforce DX project, and can deploy metadata with the Salesforce CLI. You will create a message channel as metadata, deploy it, and learn how other code refers to it.
What a message channel is in your project
A message channel is a metadata component. It lives in your source tree like an Apex class or a custom object. Publishers and subscribers do not talk to each other directly. Both refer to the same channel, so the channel file is the one place that defines the contract between them.
The channel file goes in the messageChannels folder of your package directory. The file name has the form <Name>.messageChannel-meta.xml.
Writing the channel file
Create this file at force-app/main/default/messageChannels/RecordSelected.messageChannel-meta.xml.
<?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>Each element has a job:
| Element | Purpose |
|---|---|
| `fqn` | The channel's API name, without the `__c` suffix. |
| `description` | Explains when the channel is used. |
| `isExposed` | When `true`, components in other namespaces can use the channel. |
| `lightningMessageFields` | One block per field the message carries. |
| `fieldName` | The name of a field in the payload. |
| `masterLabel` | The label shown in setup. |
Each lightningMessageFields block documents one field. Add one block for every key you plan to send.
Deploying the channel
Deploy it like any other metadata. From the project root, run:
sf project deploy start --source-dir force-app/main/default/messageChannelsIf the channel is part of a larger change, include it in the same deployment as the components that use it. Components that import a channel that does not exist in the org fail to deploy or fail to load.
Referring to the channel from code
After deployment, the platform adds the __c suffix to the channel name. This is the most common source of mistakes in this chapter. The file says RecordSelected, but code says RecordSelected__c.
An LWC imports the channel through the @salesforce/messageChannel scoped module:
import RECORD_SELECTED from '@salesforce/messageChannel/RecordSelected__c';The imported value is a reference to the channel. You pass it to publish and subscribe in later chapters. An Aura component uses the same name in the type attribute of lightning:messageChannel, for example type="RecordSelected__c".
Common errors
- Wrong folder. If the file is outside
messageChannels, the deploy does not treat it as a channel. - Missing suffix in code. Importing
@salesforce/messageChannel/RecordSelectedfails. UseRecordSelected__c. - Channel not deployed first. Deploy the channel before, or together with, the components that import it.
- Sending undeclared fields. A field that is not listed in the channel is not part of the documented contract. Declare every field you send, so that other developers can read the channel and know what to expect.
- Changing field names later. Subscribers read fields by name. Renaming a field breaks them silently, because they receive
undefined. Add new fields instead, and retire old ones only after you update every subscriber.
Check your understanding
Exercise
A channel file is named CartUpdated.messageChannel-meta.xml and has fqn="CartUpdated". What path do you use in the LWC import, and why does a deploy of the component fail if you leave out the suffix?
Show answer
Use @salesforce/messageChannel/CartUpdated__c. The platform appends __c to the channel's API name when it is deployed, so the import must include it. Without the suffix the module path does not match any channel and cannot be resolved.
Exercise
You need to send a record Id and a record type from a channel that currently declares only recordId. What do you change in the channel file, and what do you do about existing subscribers?
Show answer
Add a second lightningMessageFields block with fieldName set to recordType, then deploy the channel. Existing subscribers keep working because recordId is unchanged. They ignore the new field until you update them to read it.