Sub topic 11
Bridging LWC, Aura, and Visualforce
This chapter assumes you can already publish and subscribe from a Lightning web component (LWC) and have deployed the RecordSelected message channel from earlier chapters. Here you connect the same channel to Aura components and Visualforce pages.
One channel, three technologies
A message channel is a metadata contract, so it does not care which technology reads it. Any LWC, Aura component, or Visualforce page that references RecordSelected__c can publish to it or subscribe to it. This helps when you are migrating a page piece by piece from Aura or Visualforce to LWC.
Subscribing from Aura
Aura uses the lightning:messageChannel tag instead of the subscribe function. The tag subscribes when the component loads and unsubscribes when it is destroyed, so you do not write lifecycle code.
<aura:component implements="flexipage:availableForAllPageTypes">
<aura:attribute name="selectedId" type="String"/>
<lightning:messageChannel
type="RecordSelected__c"
aura:id="recordSelectedChannel"
onMessage="{!c.handleMessage}"
scope="APPLICATION"/>
<p>Selected: {!v.selectedId}</p>
</aura:component>The controller receives a message object. Read each payload field with getParam.
({
handleMessage: function (component, message) {
if (message != null && message.getParam('recordId') != null) {
component.set('v.selectedId', message.getParam('recordId'));
}
}
})Note the differences from LWC. The type attribute takes the channel name with the __c suffix, with no import. The scope="APPLICATION" attribute plays the role of APPLICATION_SCOPE. Without it, the Aura component only receives messages while it is the active tab or item in a navigation context, just like the default scope in LWC.
To publish from Aura, call publish on the tag's component reference and pass a plain object:
component.find('recordSelectedChannel').publish({ recordId: '001000000000001AAA' });Subscribing from Visualforce
Visualforce has no tag. It uses the global $MessageChannel merge variable to resolve the channel name, and the sforce.one object to subscribe. You manage the subscription yourself.
<apex:page>
<p>Selected: <span id="selected"></span></p>
<script>
const recordSelectedChannel = "{!$MessageChannel.RecordSelected__c}";
let subscription = null;
function onMessage(message) {
document.getElementById('selected').textContent = message.recordId;
}
function subscribeMC() {
if (!subscription) {
subscription = sforce.one.subscribe(
recordSelectedChannel,
onMessage,
{ scope: 'APPLICATION' }
);
}
}
function unsubscribeMC() {
if (subscription) {
sforce.one.unsubscribe(subscription);
subscription = null;
}
}
subscribeMC();
window.addEventListener('beforeunload', unsubscribeMC);
</script>
</apex:page>Publishing works the same way: call sforce.one.publish(recordSelectedChannel, { recordId: '...' }).
Common errors
| Symptom | Likely cause |
|---|---|
| Aura component never fires `onMessage` | `scope="APPLICATION"` is missing and the component is not in the active area |
| Visualforce `$MessageChannel` renders empty | The channel is not deployed, or the name lacks `__c` |
| Payload fields are `undefined` in Aura | You read `message.recordId` instead of `message.getParam('recordId')` |
| Duplicate messages from Visualforce | `subscribeMC` ran twice without a guard on the subscription variable |
Keep payloads compatible
All three technologies read the same fields. Keep payloads as plain objects with primitive values. If you rename a field in the channel metadata, update every Aura, Visualforce, and LWC consumer together, because nothing validates them at deploy time.
Exercise
An Aura component on a Lightning page subscribes with lightning:messageChannel but receives no messages published by an LWC in another region of the page. The channel name is correct. What is the most likely fix, and why?
Show answer
Add scope="APPLICATION" to the lightning:messageChannel tag. The default scope limits delivery to the active area of the page, so a subscriber outside it sees nothing. Application scope lets the component receive messages from anywhere in the application.
Exercise
Why does the Visualforce example guard subscribeMC with if (!subscription), and what does unsubscribeMC do that the Aura tag does automatically?
Show answer
The guard prevents a second subscription, which would call onMessage twice per message. unsubscribeMC releases the subscription when the page unloads. The Aura tag does this itself when its component is destroyed, while Visualforce code must do it explicitly.