Sub topic 5
Getting a MessageContext with @wire
This chapter assumes you know basic Lightning Web Components (templates, JavaScript classes, decorators) and the lifecycle hooks connectedCallback and disconnectedCallback. It also assumes you have already created a message channel. Here you will learn what a MessageContext is, how to get one, and the errors that come from getting it wrong.
What a MessageContext is
Every call to publish and subscribe needs a MessageContext. It is an object that tells Lightning Message Service which component is making the call and where that component sits on the page. The service uses this information to decide which subscribers can receive a message, and to clean up subscriptions when a component is destroyed.
You do not build a MessageContext yourself. The platform provides it through a wire adapter, which is a data source that you attach to a property with @wire.
Wiring the adapter
Import MessageContext and wire and attach the adapter to a class property:
import { LightningElement, wire } from 'lwc';
import { MessageContext } from 'lightning/messageService';
export default class ContextExample extends LightningElement {
@wire(MessageContext)
messageContext;
}Three details matter here:
MessageContextis imported fromlightning/messageService, the same module that exportspublish,subscribe, andunsubscribe.- The decorated field takes no parameters. You write
@wire(MessageContext), not@wire(MessageContext, { ... }). - The field holds the context object itself. It is not wrapped in
{ data, error }, unlike the result of@wireon an Apex method.
By convention the field is named messageContext. You pass it as the first argument to publish and subscribe.
When the context is available
A wired property is not set while the constructor runs. The platform fills it in after the component is created. In practice, this.messageContext is available by the time connectedCallback runs, which is why the subscriber in the previous chapter's outline subscribes there.
sequenceDiagramThe sequence below shows the order of events for a component that subscribes.
Publishers have an easier case. A publisher usually calls publish from an event handler, such as a click, which always runs after the context exists.
Using the context
Once the field is wired, pass it as the first argument:
import { LightningElement, wire } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import RECORD_SELECTED from '@salesforce/messageChannel/RecordSelected__c';
export default class RecordList extends LightningElement {
@wire(MessageContext)
messageContext;
handleSelect(event) {
const payload = { recordId: event.currentTarget.dataset.id };
publish(this.messageContext, RECORD_SELECTED, payload);
}
}A component that both publishes and subscribes needs only one wired field. Use the same this.messageContext for both calls.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `this.messageContext` is `undefined` in the constructor | Wired fields are set after construction | Move the code to `connectedCallback` or a later handler |
| Nothing happens when you call `publish` or `subscribe` | You passed the wrong first argument, such as the channel | Pass `this.messageContext` first, then the channel, then the payload or handler |
| Import error for `MessageContext` | Imported from `lwc` or a wrong path | Import from `lightning/messageService` |
| Subscription never fires in a utility bar component | Component is not wired the same way, or scope is wrong | Wire the context as shown, then check scope in a later chapter |
Exercise
A developer writes const ctx = this.messageContext; as a class field initializer and then calls subscribe(ctx, CHANNEL, handler) in connectedCallback. The subscription fails. Why, and what is the fix?
Show answer
A class field initializer runs during construction, before the platform sets the wired messageContext, so ctx is undefined. Remove the copy and read this.messageContext inside connectedCallback, where the wired value is available.
Exercise
Does a component that both publishes and subscribes to the same channel need two @wire(MessageContext) fields?
Show answer
No. One wired messageContext field is enough. Pass it as the first argument to both publish and subscribe.