Communicate Across the DOM
There are two ways to communicate between components that aren’t in the same DOM tree. Use Lightning message service (
lightning/messageService), or use a singleton library that follows the publish-subscribe pattern.
To communicate between components within a single Lightning page or across multiple pages, use Lightning message service to communicate over a Lightning message channel. The advantage over
pubsub is that message channels aren’t restricted to a single page. Any component in a Lightning Experience application that listens for events on a message channel updates when it receives a message. It works between Lightning web components, Aura components, and Visualforce pages in any tab or in any pop-out window in Lightning Experience. It also works across namespaces. For more information, see Communicate Across the DOM with Lightning Message Service.
In a publish-subscribe pattern, one component publishes an event. Other components subscribe to receive and handle the event. Every component that subscribes to the event receives the event. The
pubsub module restricts events to a single page.