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 Visualforce, use the $MessageChannel global variable. A message is a serializable JSON object. Examples of data that you can pass in a message include strings, numbers, objects, and booleans. A message cannot contain functions and symbols. The $MessageChannel global variable is only available in Lightning Experience.

Use Message Channels Created Within Your Org

Here's an example of using a Lightning Message Channel developed within your org.

1<apex:page>
2    <script>
3        // Load the MessageChannel token in a variable
4        var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";
5    </script>
6</apex:page>

Here, we reference a custom message channel with a formula expression {!$MessageChannel.SampleMessageChannel__c}. This expression creates a token that we assign to the variable SAMPLEMC. This token is unique to your custom message channel and can be used within the Lightning Message Service API methods. The syntax SampleMessageChannel__c refers to a custom instance of the LightningMessageChannel metadata type. The __c suffix indicates that it is custom, but note that it is not a custom object. See Create a Message Channel to learn more.

If your org has a namespace, do not include it in your message channel expression. For example, if your org’s namespace is MyNamespace, the message channel expression remains "{!$MessageChannel.SampleMessageChannel__c}".

Use Message Channels Created Outside Your Org

To use message channels from packages created by developers outside of your org, reference them with the syntax {!$MessageChannel.Namespace_name__c}. For example, if SampleMessageChannel was not local to your org and came from a package with the namespace SamplePackageNamespace, the syntax would be {$MessageChannel.SamplePackageNamespace__SampleMessageChannel__c}.