SaasGuru Logo

🎉Master Salesforce CPQ in 6 Weeks: Streamline Sales and Drive Revenue– Enroll Now! 🎉

🎉Master Salesforce CPQ in 6 Weeks: Streamline Sales and Drive Revenue– Enroll Now! 🎉
LIGHTNING MESSAGE SERVICE IN SALESFORCE

Lightning Message Service (LMS) in Salesforce

Salesforce introduced the Lightning Message Service (LMS) to promote effortless communication and seamless data exchange among Lightning components.

In this article, we will understand what Lightning Message Service is, its key features and advantages, and provide guidance on its effective implementation within the Salesforce platform.

What is Lightning Message Service?

Lightning Message Service (LMS) serves as a powerful communication framework introduced by Salesforce. The main goal of LMS is to enable effective communication between multiple lightning web components.

p2fmCVULZBCh1adeCk8 oqCGexTXnWdInEu8n8

Using LMS, you can enable conversations between Visualforce, Aura, and Lightning Web Components (LWC) on a Lightning page.

Below is the Lightning Message Service Flow diagram showcasing the communication between different components with the help of Lightning Message Channels.

dSQuMLApS9Xgkt3tdQEVjw 6nGqH3leb17Ktp7a5gzs1zZfxCZ vyfsxvWqyfnLUrb0GDqm4wjLnSMHMQlqkzCtmJusRdSWRHftG DINL07L8uBg6V45 QykACyObTV5DvvASbs3SG6

Also Read – Understanding Events in Salesforce Lightning Web Components

Key Concepts of Lightning Message Services

Publisher

The task of a publisher component involves sending messages to a specified message channel.

These messages can either carry data or not, and subscribers possess the capability to react and respond to these messages as needed.

When you’re creating a publisher component, it’s important to import the publish() function from the @salesforce/messageChannel module.

This function enables you to send messages through the Lightning Message Channel.

Subscriber

A subscriber component actively monitors a designated message channel. Whenever a message is published on this channel, all subscribed components that are listening can respond to that message.

Importantly, subscribers don’t require knowledge about the publisher’s specifics, which helps maintain a flexible and loosely coupled system.

Lightning Message Channel

Lightning web components can publish messages or data to a Lightning message channel. 

Simultaneously, any lightning component or Visualforce page subscribed to that specific Lightning message channel will receive the data or message on their respective components.

In order to harness the capabilities of the Lightning Message Service, it is necessary to establish a Lightning Message Channel.

Lightning message channel can be referenced with the suffix __c.

Please keep in mind that for Lightning Message Service to function properly, both components must be present on the same record page.

Create a Lightning Message Channel

To create a Lightning message channel, use the LightningMessageChannel metadata type.

Create a folder named ‘messageChannels’ at force-app/main/default location.

[qn0iJfzFxc1u6AosQPPszjcNYj0Xg RVRy2AVpvD0i1osqZ9k63kjSgmK5F3o Sm6kDh986gQ poOfhMirUVvWv7VX709M728GoG1RbnQ4

After the folder is established, the next step involves creating the initial Lightning message channel XML file, which should have the “.messageChannel-meta.xml” suffix.

The masterLabel tag in the “.messageChannel-meta.xml” file holds the name of the messageChannel which in our example is “contactMessageChannel”

To deploy a LightningMessageChannel into your org, create an SFDX project. Include the XML definition in the force-app/main/default/messageChannels/ directory. The LightningMessageChannel file name follows the format messageChannelName.messageChannel-meta.xml

dZGjI6doZYGsCnZ OTPEfRovnw sVG6M4G8BNioaTlNXlbK0WPHYK3L9RufHxAznkkVv6ZIbUUpN1kPl7HAGjXdX4DRpojJv4 sjpbI3oxBuk9uP 8k0mAh9VGez8fPfYqRpgLKc8Tcm

The “isExposed” tag serves the purpose of making the component available for use.

The “lightningMessageFields” tag can be used to pass specific information defining the field for example recordId.

You can use the “description” tag to add a description about your lightning message channel explaining the goal behind creating this message channel

Once the “.messageChannel-meta.xml” is ready, deploy it to your Salesforce org.

LWC Publisher Component Example

Now we will create a new Lightning Web Component (LWC) named publisherLWC

It will publish the data to the Lightning message channel.

daaujv7PFPo5kRp33Zj1s2ecle1Or lberqbxhg jeZaf0WGxg4LPp6dWF909rkKZAP844UdtVb31n8hoRyJXphxDW vE J 0opR8DY1z9ONNjfW5ZCP5A0lJs2j5XsZUgoskDC0p t

Always remember to add __c suffix in the end while importing the message channel.

LWC Subscriber Component Example

To listen and stop listening to messages on a message channel in your Lightning web component, import the message channel from @salesforce/messageChannel module and make use of the subscribe() and unsubscribe() functions offered by the Lightning message service.

BFYEAMVuESvR IWDeUQnXJgNT5pRMUjaT9XcEJAPZyLLRXqefSW7zp Dsp7F BDXod8nJSvkf5z0te ByETRoPSQNqVgRp5 PEeWX6AJzpV4nqJNyA9fG72dn1KPf98l3EI Hz mypFI

u jIU4JOU r3KZfHjFBYvZj4FOrkVSS66mPk Y5OLAPjNy2v6wZteIHyU036fnYQXkm1GDYG9ZKHwCf9edca7FkIzvMFZ6Zhiotlbr6C9BybFMddAQinrbOoQHsLhO6ZnNLO p4ehTgV

Benefits of implementing LMS

  • LMS allows components to be loosely connected, which simplifies maintenance and scalability of the components as changes made in one component don’t force modifications in others.
  • With dynamic components in place, lightning message service makes the user experience better.

Online Bootcamps India

Conclusion

The Lightning Message Service in Salesforce is a useful tool. It helps developers make applications that are interactive, quick to respond, and user-friendly.

 This tool achieves this by enabling real-time communication between different parts of the application called Lightning components.

Overall, the Lightning Message Service enhances the user experience, simplifies the development process, and encourages the building of applications that can easily grow and adapt to changing needs.

Take your Salesforce expertise to new heights with saasguru’s Online Salesforce Bootcamps. Dive deep into Salesforce, gaining hands-on application experience and achieving recognized certifications. Engage in real-world projects, from requirement analysis to solution designing and showcasing your work. 

Plus, refine your soft skills, focusing on personal branding, and mastering successful interviews. Sign up with saasguru today!

Table of Contents

Subscribe & Get Closer to Your Salesforce Dream Career!

Get tips from accomplished Salesforce professionals delivered directly to your inbox.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Unlock Your AI -Powered Assistant

Gain Exclusive Access to Your Salesforce Copilot

Related Articles

Top Dreamforce 2024 Keynote Announcements You Can’t Miss!

Welcome to our Dreamforce 2024 live blog, where we’ll cover the biggest announcements from this year’s event. Here’s what you can expect: Key insights on Salesforce’s Agentforce for autonomous agents. Major AI advancements and a $1B investment in AI technologies. Exciting partnerships with IBM and Google to extend agent capabilities. Data Cloud enhancements for faster, …

Top Dreamforce 2024 Keynote Announcements You Can’t Miss! Read More »

Dreamforce 2024 Live: Real-Time Updates, AI News, and Event Highlights

Stay updated with live insights, breaking news, and exclusive highlights from Dreamforce 2024 in San Francisco. Check now!

Choosing Between Apex and Flows: A Comprehensive Salesforce Guide

Explore the key differences between Apex and Flows in Salesforce. Discover which tool suits your automation needs best, from ease of use to performance.