Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Publish on a Message Channel
Example
To reference a message channel, add the lightning:messageChannel component to your Aura component. The component has a required type attribute, which is the name of the message channel.
1<!-- myComponent.cmp -->
2<aura:component>
3 <lightning:messageChannel type="SampleMessageChannel__c"/>
4</aura:component>To reference a message channel from an org that has a namespace, prefix the message channel name with the namespace: <lightning:messageChannel type="Namespace__MessageChannelName__c"/>.
This example shows how to publish a message on the SampleMessageChannel__c channel when a button is clicked.
In myComponent.cmp, we create two components, lightning:button and lightning:messageChannel. On lightning:button, the onclick handler calls the handleClick() JavaScript function in the controller. We assign the aura:id attribute to lightning:messageChannel to access the publish() method.
1<!-- myComponent.cmp -->
2<aura:component>
3 <lightning:button onclick="{! c.handleClick }"/>
4 <lightning:messageChannel type="SampleMessageChannel__c"
5 aura:id="sampleMessageChannel"/>
6</aura:component>1// myComponentController.js
2({
3 handleClick: function(cmp, event, helper) {
4 var payload = {
5 recordId: "some string",
6 recordData: {
7 value: "some value"
8 }
9 };
10 cmp.find("sampleMessageChannel").publish(payload);
11 }
12})In the controller, handleClick() contains the payload object. This object holds the message that gets sent on the SampleMessageChannel__c message channel. Here, the message is a recordId with the value "some string" and recordData, whose value is the key-value pair value: "some value". Then, the controller finds the lightning:messageChannel component referenced in myComponent.cmp and calls publish() with the payload.