Currently I'm playing around with the Lightning Message Service.

I've noticed tha online tutorials for using LMS with both VisualForce and Lightning Web Components demonstrate publish, subscribe, and unsubscribe.

However, tutorials for Lightning Aura Components only demonstrate publish.

Obviously I could create a boolean value controlling whether to ignore incoming messages, but that would be less efficient for the system then turning the subscription on and off, as the messages would still be transmitted and received.

Given the following component, is it possible to create functions for toggling the subscription?

<!-- lmsDemo.cmp -->
<aura:component description="LMSAuraComponentDemo" implements="flexipage:availableForAllPageTypes" access="global">
    <aura:attribute name="message" type="String"/>
    <aura:attribute name="incomingMessage" type="String"/>
    <lightning:messageChannel aura:id="myMessageChannel"

    <lightning:card title="LMS Aura Component"  iconName="custom:custom16">
        <div class="slds-m-around_medium">
            <p>MessageChannel: MyMessageChannel__c</p>
        <!-- Default/basic -->
        <div class="slds-p-around_medium lgc-bg">
            <lightning:input label="Message" value="{!v.message}"/>
            <lightning:button label="Publish" onclick="{!c.publishToMessageChannel}"/>

        <div class="slds-p-around_medium lgc-bg">
            <lightning:button label="Subscribe" onclick="{!c.subscribeToMessageChannel}"></lightning:button>
            <lightning:button label="Unsubscribe" onclick="{!c.unsubscribeToMessageChannel}"></lightning:button>

            <p>Latest Message Received</p>
            <lightning:formattedText value="{!v.incomingMessage}"/>
// lmsDemoController.js
    handleMessage: function (component, event) {
        if (event) {
            component.set('v.incomingMessage', JSON.stringify(event));

    publishToMessageChannel: function (component) {
                messageToSend: component.get('v.message'),
                sourceSystem: 'From Aura'

    subscribeToMessageChannel: function (component) {
        // TODO

    unsubscribeToMessageChannel: function (component) {
        // TODO


lightning:messageChannel handles subscribing and unsubscribing on your behalf. You only need to specify the onMessage handler:

<lightning:messageChannel type="SampleMessageChannel__c"
        onMessage="{!c.handleChanged}" scope="APPLICATION"/>

The documentation has a typo, as it then says:

    handleModelChanged: function(cmp, message, helper) { 
        // Read the message argument to get the values in the message payload
    if (message != null && message.getParam("recordData") != null) {
        cmp.set("v.recordValue", message.getParam("recordData").value);

Obviously, the method names must match, which may be why you were confused.

  • Sorry, maybe my question isn't clear. I understand that the above code is enough to establish and handle a subscription. But, if for some hypothetical reason (I don't know why), I wanted to keep the component but unsubscribe or render the component without starting the subscription, would that be possible? – Brian Kessler Jun 26 '20 at 12:57
  • @BrianKessler You could choose to dynamically set onMessage to at least start/stop handling the messages, although this wouldn't actually subscribe/unsubscribe. – sfdcfox Jun 26 '20 at 13:11
  • Understood... but that would be inefficient since other parts of the system would be wasting resources to create and transmit the message and then the component itself would need to actively ignore them. – Brian Kessler Jun 26 '20 at 13:18
  • @BrianKessler I don't know what goes on under the hood, but I'm pretty sure if there's no onMessage, there's no subscription, thus no waste of resources. – sfdcfox Jun 26 '20 at 13:24
  • 1
    @BrianKessler I could probably write a test, but will have to wait until this afternoon. – sfdcfox Jun 26 '20 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.