Publish on a Message Channel

To publish on a Message Channel from a Visualforce page, include the $MessageChannel global variable in your page's JavaScript code and write a method that calls sforce.one.publish().

Example

The lmsPublisherVisualforce page from the github.com/trailheadapps/lwc-recipes repo shows how to publish a message to notify subscribers on a Lightning page when a contact is selected.

The following example walks through the Visualforce page markup to show how to publish to a Message Channel when a button is clicked.

In the page's JavaScript, we first get a reference to the custom Lightning Message Channel with the formula expression {!$MessageChannel.SampleMessageChannel__c}. This expression creates a token that is unique to your Message Channel. We then assign the token as a string to the variable SAMPLEMC.

The function handleClick() contains the message content that we want to publish. Here, the message is a recordId with the value "some string" and recordData, whose value is the key-value pair value: "some value". We then call the Lightning Message Service API's publish() method on the sforce.one object. The publish() function takes two parameters, a string containing the Message Channel token and the message payload.

In the page markup, we create a button and call handleClick() on its onclick() method.

1<apex:page >
2    <script>
3    // Load the MessageChannel token in a variable
4    var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";
5    function handleClick() {
6        const payload = {
7            recordId: "some string",
8            recordData: {value: "some value"}
9        }
10        sforce.one.publish(SAMPLEMC, payload);
11      }
12    </script>
13    <div>
14    <p>Publish SampleMessageChannel</p>
15    <button onclick="handleClick()">Publish</button>
16    </div>
17</apex:page>