Create a Message Channel

To create a Lightning message channel, use the LightningMessageChannel metadata type.

Define Message Channel Metadata in Your Org 

To deploy a LightningMessageChannel into your org, create an SFDX project. Include the XML definition in the force-app/main/default/messageChannels/ directory. The LightningMessageChannel file name follows the format messageChannelName.messageChannel-meta.xml.

For an example metadata file, see Metadata API Developer Guide: LightningMessageChannel.

To add it to your scratch org or another type of org, such as a sandbox or a Developer Edition org, run sf project deploy start.

Import a Message Channel 

Here’s how to import a Lightning message channel that a component can use to communicate via the Lightning Message Service.

1// Basic syntax
2import channelName from "@salesforce/messageChannel/channelReference";
3
4// Syntax for resources in a managed package
5import channelName from "@salesforce/messageChannel/namespace__channelReference";
1// Examples
2import recordSelected from "@salesforce/messageChannel/Record_Selected__c";
3import recordSelectedFromPackage from "@salesforce/messageChannel/namespace__Record_Selected__c";

Record_Selected__c refers to a custom instance of the LightningMessageChannel metadata type. Although it uses the __c suffix, it isn’t a custom object.

Note

  • channelName—An imported symbol that identifies the message channel.
  • channelReference—The API name of the message channel in the format Channel_Name__c.
  • namespace—If the message channel is in a managed package, this value is the namespace of the managed package. If the message channel is not in a managed package, do not include a namespace.

The lwc-recipes repo has a messageChannel-meta.xml example.

Tip