Before beginning code, we have to build a Message Channel. The only method to do this is by leveraging the Metadata API. Salesforce will ultimately provide a friendly UI for it, but we have to do a little work right now.
Initially, we will build a zip file containing both a package.xml and a file describing the name and fields for our Message Channel. This file should be within a folder called messageChannels, and the setup for the filename is CHANNELNAME.messageChannel.
In our instance, that means SayWhat.messageChannel.
Go to the subsequent screen in workbench, click “Deploy,” and then obey the commands. In the end, you’ll have an appropriately deployed Message Channel!
We’re going to manage Visualforce to begin with. In the markup, we will install a simple textbox/button order to transfer messages and two buttons/textbox to manage the subscribe/unsubscribe and decoding of messages.
To declare a message to the channel, we build a JS function that produces a JSON object, and then we call the sforce.one.publish process with it as a parameter. My JSON object features agree with the names of the fields involved in the Message Channel, and that none of them uses __c.
To subscribe and unsubscribe, we use a related method. We make a call to a process in the sforce.one namespace. The subscribe process returns an object that we will require to unsubscribe later. The parameter on that event will include a JSON object with the same shape.
Aura is a little distinctive from VF. Salesforce did not add a subscribe/unsubscribe functionality here; instead, of automatically doing it when we place the lightning:messageChannel segment on the page. The onMessage event will let us understand when something has been published.
To publish, we simply create a JSON object and transfer it to the publish method of the segment we defined in markup.
The JSON’s parts come as the event variable’s parameters, so we have to use getParam to see the values into variables. Using event.propertyname will yield an undefined value.
In review, LMS presents us with a rapid and straightforward way to move complicated data across the DOM between VF and LWC/Aura, enabling greater interoperability. There are several cost-saving possibilities in retrofitting old VF pages to run with new Lightning elements.