Newer Version Available

This content describes an older version of this product. View Latest

Communicating Across the DOM with Lightning Message Service (Developer Preview)

Use the Lightning Message Service API to communicate across the DOM, between Aura components, Visualforce pages, and Lightning web components. If you're switching from Salesforce Classic to Lightning Experience, you can now build Lightning web components that can communicate with existing Visualforce pages or Aura components.

This feature is available as a developer preview for Developer Edition and scratch orgs. It isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements.

Note

To access Lightning Message Service in Aura, use the lightning:messageChannel component. A message is a serializable JSON object. Examples of data that you can pass in a message include strings, numbers, booleans, and objects. A message cannot contain functions and symbols. The lightning:messageChannel component is only available in Lightning Experience.

Message Channels Created Within Your Org

To reference a message channel developed in your org, add the lightning:messageChannel component to your Aura component. The component has two required attributes, aura:id and type. The aura:id is the local ID of the component. The type is the name of the message channel that the lightning:messageChannel component references. To create a custom instance of the LightningMessageChannel metadata type, use the __c suffix. Note that this is not a custom object, it just uses the same suffix. Here, we reference SampleMessageChannel__c.

1<!-- myComponent.cmp -->
2<aura:component>
3<lightning:messageChannel type="SampleMessageChannel__c"
4    aura:id="sampleMessageChannel"/>
5</aura:component>

If your org has a namespace, then prefix the type attribute with the namespace. For example, if your org's namespace is MyNamespace, then use the syntax <lightning:messageChannel type="MyNamespace__SampleMessageChannel__c" aura:id="sampleMessageChannel"/>.

Message Channels Created Outside Your Org

To use a message channel from a package created by a developer outside of your org, use the following syntax: <lightning:messageChannel type="Namespace__name__c" aura:id="local_id"/>. For example, if SampleMessageChannel__c was not local to your org and came from a package with the namespace SamplePackageNamespace, the syntax would be <lightning:messageChannel type="SamplePackageNamespace__SampleMessageChannel__c" aura:id="sampleMessageChannel"/>.