Imagine you’re at a bustling Salesforce App Café. Each table represents a component—some tables have families sitting together (parent-child components), others have strangers shouting across the room (cross-DOM communication), and a few people are still using old walkie-talkies (pub-sub model). In the world of Lightning Web Components (LWC), effective communication patterns are the difference between a harmonious café and absolute chaos.
Today, let’s break down Custom Events, LMS, and the fading legacy of Pub-Sub.
🛠️ Custom Events: The Parent-Child Relationship
Imagine a parent and child sitting at the same table. They communicate directly—one asks, and the other responds. There’s trust, immediacy, and contextual clarity.
How It Works:
-
The Child says: “Mom, I clicked the button!” (dispatches a Custom Event).
-
The Parent responds: “Thanks, let me handle that.” (listens and acts).
When to Use It:
-
Components share a direct, hierarchical relationship (Parent ↔ Child).
-
Communication happens in a one-to-one, predictable pattern.
Example: A child component triggers an event when a button is clicked, and the parent captures that event.
💡 Insight:
Just like a healthy parent-child relationship, boundaries are clear, and trust is implicit. Custom Events work best when there’s a direct line of communication. They foster a smooth flow of information where you can easily manage interactions, keeping things straightforward and efficient.
🛰️ Lightning Message Service (LMS): The Public Broadcast System
Imagine a public address system in a crowded marketplace. Announcements are made, and everyone who’s tuned in listens. Some respond, some ignore, but the message travels far and wide.
How It Works:
-
LMS uses Lightning Message Channels (LMC) to broadcast messages.
-
Components in different DOM trees, across Aura ↔ LWC, or even different namespaces can exchange messages.
When to Use It:
-
Communication needs to happen across unrelated components.
-
Components span different DOM trees or frameworks (Aura ↔ LWC).
Example: A notification from a utility bar component updates components on a Lightning record page.
💡 Insight:
LMS is like a city-wide radio network—it’s structured, reliable, and Salesforce-approved. This method is especially great for larger applications where many components need to stay in sync without having a direct line of communication to each other. It’s the modern network that helps avoid bottlenecks and keeps messages flowing smoothly.
📻 Pub-Sub Model: The Aging Walkie-Talkie
Think of two kids in treehouses using walkie-talkies. It worked when they were close, but now they’ve outgrown this system. The walkie-talkies are scratchy, unreliable, and outdated.
How It Works:
-
Components use a shared event bus (like a middleman messenger).
-
One component publishes an event, and subscribed components react.
When to Use It:
-
Only when Lightning Message Service (LMS) isn’t supported.
-
In legacy environments or unsupported containers.
Example: Temporary fixes in older Salesforce applications.
💡 Insight:
Salesforce has made it clear—Pub-Sub is no longer actively supported or maintained. If you’re still using it, it’s time to upgrade to LMS. This is like holding onto an old, broken toy that you’ve outgrown—it may have served you well once, but it’s time to let it go. You deserve better!
📝 The Psychology Behind Choosing the Right Communication Method
Let’s think of this decision as choosing the right conversation style for your situation:
Scenario |
Preferred Method |
Analogy |
Why? |
---|---|---|---|
Parent ↔ Child Component |
Custom Events |
Family Conversation |
Direct & Trusted |
Cross-DOM Communication |
LMS |
Public Broadcast |
Scalable & Reliable |
Aura ↔ LWC Communication |
LMS |
Bridging Frameworks |
Native Integration |
Namespace Boundary Sharing |
LMS |
Cross-Team Collaboration |
Secure & Supported |
Unsupported Containers |
Pub-Sub (Legacy) |
Outdated Walkie-Talkies |
Temporary Fix |
🏆 LMS: The Clear Psychological & Technical Winner
-
Custom Events: Best for local, family-style communication (Parent ↔ Child).
-
LMS: The city-wide communication network for scalable, cross-boundary messaging.
-
Pub-Sub: The dusty walkie-talkie in the attic—use it only if you must.
💡 Insight:
In any communication system—human or technical—clarity, trust, and structure are key. LMS embodies these principles for Salesforce developers. It ensures you’re not just talking to the wind; you’re ensuring the right person hears your message clearly and effectively.
So, whether you’re a Salesforce Architect designing a complex application or a psychologist analyzing social dynamics, remember: choose the communication tool that fits the conversation you’re having.
In today’s Salesforce world, LMS isn’t just the recommended choice—it’s the smart choice. 🚀