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"
type="MyMessageChannel__c"
scope="APPLICATION"
onMessage="{!c.handleMessage}"
/>
<lightning:card title="LMS Aura Component" iconName="custom:custom16">
<div class="slds-m-around_medium">
<p>MessageChannel: MyMessageChannel__c</p>
<br/>
</div>
<!-- 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>
<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}"/>
</div>
</lightning:card>
</aura:component>
// lmsDemoController.js
({
handleMessage: function (component, event) {
if (event) {
component.set('v.incomingMessage', JSON.stringify(event));
}
},
publishToMessageChannel: function (component) {
component.find('myMessageChannel')
.publish({
messageToSend: component.get('v.message'),
sourceSystem: 'From Aura'
});
},
subscribeToMessageChannel: function (component) {
// TODO
},
unsubscribeToMessageChannel: function (component) {
// TODO
}
});