Sub topic 10
Designing message payloads
This chapter assumes you can publish and subscribe on a message channel from a Lightning web component (LWC). You should also know how connectedCallback and disconnectedCallback work, and you should have deployed a message channel as metadata. Here you will learn how to decide what goes into the payload and how to avoid the errors that come from a poorly shaped one.
What a payload is
A payload is the plain JavaScript object you pass as the third argument to publish. Every subscriber to the channel receives it as the message argument of its handler. The fields you declare in the channel's lightningMessageFields document the contract between publisher and subscribers.
publish(this.messageContext, RECORD_SELECTED, { recordId: '001xx000003DGb2AAG' });The payload is the only thing the two sides share. They do not share a parent, a template, or a class, so the payload must carry enough meaning for the subscriber to act without asking the publisher anything.
Keep payloads small and serializable
LMS copies the payload when it delivers it. It can also cross the boundary between LWC, Aura, and Visualforce, so the payload must be data that survives that trip.
Follow these rules:
- Use strings, numbers, booleans,
null, plain objects, and arrays of those. - Do not send functions, DOM nodes, component instances, or class instances. They do not serialize, so they are lost or cause errors.
- Do not send large record sets. Send identifiers and let the subscriber load what it needs.
- Do not send a reactive proxy you intend to keep mutating. Build a fresh object for each message.
Sending an Id is usually better than sending the whole record. Each subscriber can then fetch only the fields it needs, through getRecord or an Apex method, and it always reads current data.
// Prefer this
const payload = { recordId: event.currentTarget.dataset.id };
// Avoid this: the record can go stale, and every subscriber receives all of it
const heavyPayload = { record: this.selectedRecord };Declare every field in the channel
Add each field you send to the metadata file. The declaration is documentation for other developers, and it makes the contract visible in one place.
<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata" fqn="RecordSelected">
<description>Published when a user selects a record in a list.</description>
<isExposed>true</isExposed>
<lightningMessageFields>
<description>Id of the selected record</description>
<fieldName>recordId</fieldName>
</lightningMessageFields>
<lightningMessageFields>
<description>API name of the object, such as Account</description>
<fieldName>objectApiName</fieldName>
</lightningMessageFields>
<masterLabel>Record Selected</masterLabel>
</LightningMessageChannel>The field list is a convention, not a type check. LMS does not reject a message that has extra or missing fields, so a typo such as recordID fails silently: the subscriber just reads undefined.
Design for the subscriber you cannot see
Any component on the page can subscribe to a channel, including components written later by other teams. Design with that in mind.
- Describe an event, not a command. Name the payload for what happened (
recordIdof the selected record), not what a subscriber must do (openTab: true). Each subscriber decides its own response. - Include only what most subscribers need. If one subscriber needs extra data, it can look that data up from the Id.
- Add fields rather than change them. Existing subscribers ignore new fields. Renaming or removing a field breaks them.
- Guard in the handler. Check that the value you need is present before using it.
handleMessage(message) {
if (message && message.recordId) {
this.selectedId = message.recordId;
}
}Common errors
| Symptom | Likely cause |
|---|---|
| Subscriber reads `undefined` | Field name in the payload does not match what the subscriber reads |
| Data is out of date | The payload carried a copy of a record instead of an Id |
| Aura component gets no value | The Aura handler read the wrong key with `getParam` |
| Behavior breaks after a release | A field was renamed or removed |
Exercise
A list component publishes the full Account record in the payload. A detail component shows stale data after another component edits the record. How would you redesign the payload, and why?
Show answer
Publish only the Id (and, if useful, the object API name), for example { recordId, objectApiName }. The detail component then loads the record itself with getRecord or Apex, so it reads current data. The smaller payload also serializes safely and exposes no record fields to other subscribers.
Exercise
You rename the payload field recordId to id in the publisher. No error appears, but the subscribers stop working. Why, and what should you have done?
Show answer
LMS does not validate payload fields against the channel metadata, so subscribers simply read undefined from message.recordId. Add the new field and keep the old one for a transition period, then remove the old one after all subscribers are updated.