Sub topic 9
Default scope versus APPLICATION_SCOPE
This chapter assumes you can already publish and subscribe with lightning/messageService, and that you know connectedCallback and disconnectedCallback. Here you decide which published messages a subscriber receives.
What scope controls
A message channel does not decide who hears a message. The subscriber's scope does. Scope is an option you pass as the fourth argument to subscribe(). It matters in Lightning Experience apps that keep several areas open at once, such as console apps with multiple workspace tabs and a utility bar.
There are two choices:
- Default scope. The subscriber receives messages only while its component is in the active area of the app. That area is the currently selected tab or navigation item, or an open utility item.
- APPLICATION_SCOPE. The subscriber receives messages from any publisher in the application, including components in background tabs.
Default scope
If you omit the options argument, you get default scope.
this.subscription = subscribe(
this.messageContext,
RECORD_SELECTED,
(message) => this.handleMessage(message)
);This is the safer choice when each tab is a self-contained workspace. Suppose an agent has two Case tabs open and each tab contains a record list and a detail panel. Selecting a record in tab A should not change the detail panel in tab B. Default scope gives you that behavior without extra code.
APPLICATION_SCOPE
Import the APPLICATION_SCOPE constant and pass it in the options object.
import {
subscribe,
APPLICATION_SCOPE,
MessageContext
} from 'lightning/messageService';
this.subscription = subscribe(
this.messageContext,
RECORD_SELECTED,
(message) => this.handleMessage(message),
{ scope: APPLICATION_SCOPE }
);Use this when the subscriber and the publisher are not in the same active area. Common cases:
- A utility bar item, such as a notes panel, that must react to a record selected in a workspace tab.
- A component in a background tab that should stay current while the user works elsewhere.
- A shared navigation or status component that reflects activity from the whole app.
The Aura equivalent is scope="APPLICATION" on lightning:messageChannel, as in the Aura example from the spine.
How the choice plays out
Choosing a scope
| Situation | Scope |
|---|---|
| Publisher and subscriber both live in the same page or tab | Default |
| Each tab should behave independently | Default |
| Subscriber is in a utility item or another navigation area | APPLICATION_SCOPE |
| Subscriber must stay current while in a background tab | APPLICATION_SCOPE |
Common errors
The most frequent mistake is a subscriber that works on a single record page and fails in a console app. In the console, the publisher and subscriber sit in different areas, and the default scope filters the message out. Nothing throws an error. The handler is simply never called.
A second mistake is using APPLICATION_SCOPE everywhere "just in case". Every open tab with a subscriber now reacts to every publish. Users see detail panels change in tabs they are not looking at. Choose the narrowest scope that works.
Scope and the lifecycle
Scope does not change the lifecycle rules. Subscribe in connectedCallback, keep the returned subscription, and unsubscribe in disconnectedCallback. With APPLICATION_SCOPE this matters more. A component that is never unsubscribed can keep receiving messages from the whole application after it has left the page.
Exercise
A notes component sits in the utility bar of a console app. It must show the record a user selects in any workspace tab. Which scope should it use for its subscription, and what happens if you leave the options argument out?
Show answer
It should use APPLICATIONSCOPE, passed as { scope: APPLICATIONSCOPE } in the fourth argument to subscribe(). With default scope, the utility item is not in the same active area as the publishing tab, so it does not receive the messages and the handler never runs.
Exercise
Why is it a poor idea to give every subscriber APPLICATION_SCOPE?
Show answer
Every open tab with a subscriber then reacts to every published message, including ones from tabs the user is not viewing. Independent workspaces start affecting each other. Use default scope unless the subscriber must hear publishers outside its own active area.