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.
Subscribe to a Message Channel
Example
In this example, we define an Aura component called myNewComponent that contains the custom message channel, SampleMessageChannel__c. The lightning:messageChannel component's onMessage attribute calls the handleChanged method in the client-side controller.
- Standard navigation tabs
- Console navigation workspace tabs
- Console navigations subtabs
- Console navigation items
1<lightning:messageChannel type="messageChannel" onMessage="{!listener}" scope="APPLICATION"/>The component myNewComponent detects a new message and updates the display value.
1<!-- myNewComponent.cmp -->
2<aura:component>
3 <aura:attribute name="recordValue" type="String"/>
4 <lightning:formattedText value="{!v.recordValue}" />
5 <lightning:messageChannel type="SampleMessageChannel__c"
6 onMessage="{!c.handleChanged}"/>
7</aura:component>1// myNewComponentController.js
2({
3 handleChanged: function(cmp, message, helper) {
4 // Read the message argument to get the values in the message payload
5 if (message != null && message.getParam("recordData") != null) {
6 cmp.set("v.recordValue", message.getParam("recordData").value);
7 }
8 }
9})Write the handler in your component's client-side controller. The handleChanged method fires when there is a new message. It checks whether there is a payload in the message, and if so, assigns the new data to the v.recordValue attribute. The lightning:formattedText element updates to display the new value.