Developers

Lightning Message Service (LMS) vs. Aura/Visualforce

By Kateryna Boiko

Lightning Message Service (LMS) benefits not only Visualforce coders, but Aura and LWC programmers too. The feature was previewed in the Salesforce Winter ‘20 release, then became generally available half a year later, in the Summer ‘20 release.

What is Lightning Message Service?

Lightning Message Service is a unique Salesforce feature that enables communication between Visualforce, Aura, and Lightning Web Components on the same Lightning Page. It’s worth noting that LMS is available in Lightning Experience only.

LMS can be viewed as a standard publish-subscribe library in the UI that allows components in any part of the DOM to communicate instantly through simple API. This API allows publishing messages through Lightning Experience, as well as subscribing to them, no matter where they originate in Lightning Experience.

This secure communication channel is a beneficial option to both in-house and freelance Salesforce developers, as it allows for seamless communication across the Salesforce UI tech stack, including Visualforce Pages, Aura components, and Lightning Web Components (LWC).

In this article, we break down the specifics and limitations of LMS, how it can be used with Aura and Visualforce components, and why organizations are eager to hire Salesforce developers with LMS knowledge.

LMS is based on a new type of metadata called Lightning Message Channels. These new channels are lightweight, packageable components that one can create in an org and at runtime, publish them, and subscribe to messages on them.

Here are the benefits of Lightning Message Service:

  • Since this metadata type is packageable, it’s possible to associate a message channel to a specific namespace and make it available/unavailable to other namespaces.
  • Decreased development time, as if a Visualforce Page or Lightning component attempts to reference a non-available message channel, the code won’t even compile.
  • Referential integrity between message channels and the code that references them.

How to Use Lightning Message Service

LMS is a front-end solution that operates in client-side UI, supporting popped out utility bar item windows, as well as parent/child iFrame windows (mostly related to Visualforce). It’s important, however, that all interactions should originate from the same Lightning Experience app (i.e. same browser tab).

While Aura Application Events support the communication between components no matter where they are located in the DOM, LMS takes the communication to a whole new level. LMS is a crucial step towards the advanced Lightning ecosystem for two main reasons:

  • Following the rising adoption of Lightning Web Components, LMS is now being positioned as the main publish-subscribe library in the UI that provides a standard means for LWC to communicate with Aura Components.
  • Including the fact that it speaks to Visualforce as well, LMS is a must-have solution for Salesforce experts.

It’s worth mentioning that LMS does have some limitations; currently, it is not possible to use LMS under the conditions below:

  • AppExchange
  • Salesforce Mobile
  • Communities

However, taking into consideration the new-level communication opportunities that LMS offers, as well as strong support from the Salesforce community, we can state with certainty that these limitations are either minor or temporary.

The demand for Salesforce solutions is growing, and so is the cost of a Salesforce developer. Rising demand for Salesforce developers reflects the global market interest in expertise in Visualforce and Lightning Components (Aura/LWC) components, and now, Lightning Message Service as well.

Besides tech specifics, here’s a real-life example of how LMS can be beneficial for organizations. Let’s say, a company considers migrating to Lightning Experience that provides access to the newest tech stack, including Aura Components and LWC. Lightning Message Service is what can turn this migration from a cumbersome task to a much more convenient process.

Thanks to the fact that with LMS, it’s possible to communicate between Visualforce Pages and Aura/LWC components, a company can proceed with a more organized upgrade approach. Instead of processing all the existing Visualforce Pages and then creating Lightning Components from scratch, Salesforce programmers can simply set up the connection between Visualforce and Lightning Components with the help of LMS to develop the new advanced functionality.

Lightning Message Service Vs. Aura/Visualforce

Below is a sample definition for a SampleMessageChannel message channel that is mentioned in the Metadata API Developer Guide. The only required field here is the masterLabel that identifies this message channel in different user interfaces. The isExposed boolean field is optional and is false in the example since it’s not specified. It communicates to the system whether this message channel is available to components in other namespaces or not.

Source: Lightning Message Service Guide

In short, it is possible to save the Message Channel to your org using the Salesforce CLI and then use it in a Visualforce page or Lightning component. However, in this article, we will take a deeper look at the use of LMS with Visualforce and Aura Components.

To allow LMS to communicate with Visualforce, new sforce.one APIs were introduced. These APIs allow for three main functions – publish, subscribe, and unsubscribe.

Below is an example of a sample Visualforce page, which references the SampleMessageChannel LMS, as per the Visualforce Developer Guide. The first parameter is the message channel reference that is received from the $MessageChannel global variable put in the {! } formula, while the second parameter is the message payload that uses publish. The publishMC function is connected to a button click in the rest of the HTML.

Source: Lightning Message Service Guide

Let’s now look at how LMS allows communication with Aura Components. In the example below taken from the Lightning Aura Components Developer Guide, we reference a message channel in the Aura Component by adding the lightning:messageChannel component to it and publish it. The type attribute gets the name of the referenced message channel.

Source: Lightning Message Service Guide

More or less, LMS is similar to Application Events for Aura Components. With such events, it was possible to define an Aura component containing the fields, which stored the data processed as part of the event. Programmers then could use these to pass events to components that have registered for the event.

In LMS, the equivalent to this workflow is a Message Channel that is not a component but a new metadata type that contains tags for defining the name and data fields for the event.

For more information, it’s worth checking the detailed Lightning Aura Components Developer Guide for Salesforce developers, namely Communicating Across the DOM with Lightning Message Service, that includes examples on:

  • How to create a Message Channel
  • How to publish on a Message Channel
  • How to subscribe to a Message Channel
  • Specifics and limitations of Message Channels

Source: Lee Campbell from Pexels

Summary

If your organization is considering upgrading to Lightning Experience, or you are already using Lightning Experience and are interested in new functionality available, LMS is the must-try feature.

Should there be a time you wanted Visualforce Pages to communicate with other Visualforce Components, Aura Components, or LWC located on the same Lightning Page, now it’s possible, thanks to LMS.

The Author

Kateryna Boiko

Kateryna Boiko is a Marketing Director at Mobilunity, Provider of Dedicated Development Teams with 9 years of hands-on experience in digital marketing.

Comments:

    Anurag
    September 03, 2021 11:55 am
    insightful article, thanks! It has a small typo I think under “benefits”. “Increased development time, as if a Visualforce Page or Lightning component attempts to reference a non-available message channel, the code won’t even compile.” “Increased” should be “decreased/reduced”?
    Christine Marshall
    October 04, 2021 9:33 am
    Thanks for spotting and letting us know!

Leave a Reply