---
schema_version: "1"
id: 8585b3cc36a1442a04e28d9b9e42d7745cf91427842871fe878590b9f45619d2
title: Publish on a Message Channel
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/message-channel/message-channel-publish.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/message-channel-publish.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 008c0d16daa3be0ad8211d46433277e37d5af1a72f6e648c46e91ffa44552e24
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# 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](https://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.

```nolang
<apex:page >
    <script>
    // Load the MessageChannel token in a variable
    var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";
    function handleClick() {
        const payload = {
            recordId: "some string",
            recordData: {value: "some value"}
        }
        sforce.one.publish(SAMPLEMC, payload);
      }
    </script>
    <div>
    <p>Publish SampleMessageChannel</p>
    <button onclick="handleClick()">Publish</button>
    </div>
</apex:page>
```

