Advanced 20 min read

Sibling Communication with Lightning Message Service

By the end of this lesson, you'll be able to:

  • Explain why sibling (non-nested) components need a different communication mechanism
  • Describe the publish/subscribe model of Lightning Message Service
  • Use the basic LMS API to publish and subscribe to a message

Prerequisites: "Event Propagation: bubbles, composed, and Retargeting"

The Sibling Problem

@api and CustomEvent both rely on a direct parent/child relationship — a parent passing data down, or a child's event bubbling up to a listening ancestor. Two components that are not nested — for instance, two independent components both placed directly on the same Lightning Record Page — have no such relationship to lean on.

Lightning Message Service (LMS)

Lightning Message Service solves this with a publish/subscribe model, built around a Lightning Message Channel — a piece of metadata that defines a named "channel" any component can publish to or subscribe from, regardless of where it sits in the page.

Basic LMS Usage

// Publishing (in one component)
import { LightningElement, wire } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import RECORD_SELECTED_CHANNEL from '@salesforce/messageChannel/RecordSelected__c';

export default class FilterPanel extends LightningElement {
    @wire(MessageContext) messageContext;

    handleFilterClick(recordId) {
        publish(this.messageContext, RECORD_SELECTED_CHANNEL, { recordId });
    }
}
// Subscribing (in a completely unrelated component)
import { subscribe, MessageContext } from 'lightning/messageService';
import RECORD_SELECTED_CHANNEL from '@salesforce/messageChannel/RecordSelected__c';

connectedCallback() {
    subscribe(this.messageContext, RECORD_SELECTED_CHANNEL, (message) => {
        this.selectedRecordId = message.recordId;
    });
}

@wire(MessageContext) (a first glimpse of the wire service, fully covered in Module 6) gives access to the messaging infrastructure this all runs on top of.

Why This Matters in Real Projects

Real Lightning pages commonly have several independent components — a filter panel, a results list, a detail panel — that all need to coordinate without being nested inside each other. LMS is the standard, supported way to make that happen.

Exercise

Explain, as a comment, why two sibling components placed independently on the same Record Page cannot simply use @api to share data.

Show hint

Think about what relationship @api actually requires.

JAVASCRIPT

Exercise

Challenge: name the LMS function used to send a message, and the one used to receive it.

Show hint

Both are imported from lightning/messageService.

JAVASCRIPT

Sibling Communication with Lightning Message Service Quiz

1. What communication mechanism is designed specifically for unrelated (non-nested) components?

2. What is a Lightning Message Channel?

3. A parent/child relationship is required to use Lightning Message Service.

4. What does publish() do in the LMS API?

5. What gives a component access to the underlying messaging context needed for LMS?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

@api and CustomEvent only work along a direct parent/child relationship — Lightning Message Service is how two unrelated components on the same page coordinate with each other.