---
schema_version: "1"
id: 55172952c32acb5d6e3cf32734d89c36458860bf37a4b9027be3fd9b555d2f5b
title: Subscribe and Unsubscribe from 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-subscribe.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/message-channel-subscribe.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: b98870858652bfe4b04052756a99045f510844dccd676500d3f8fce48c14aa43
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# Subscribe and Unsubscribe from a Message Channel

To subscribe and unsubscribe from a message channel, use the `sforce.one.subscribe()` and `sforce.one.unsubscribe()` methods.


## Example

The `lmsSubscriberVisualforceRemoting` page from the [github.com/trailheadapps/lwc-recipes](https://github.com/trailheadapps/lwc-recipes) repo shows how to subscribe and unsubscribe from a message channel.

The following example is a continuation of the example in [Publish on a Message Channel](https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/message-channel-publish.md) that lets you subscribe and unsubscribe from a message channel when you click the respective button. In the JavaScript, we have the `subscribeMC()` and `unsubscribeMC()` methods, and `onMCPublished()` to populate the `textarea` with message output.

Load the custom message channel `$MessageChannel.SampleMessageChannel__c` into the variable `SAMPLEMC`. The `$MessageChannel` global variable creates a unique token for the associated message channel. Under `SAMPLEMC`, we declare the variable `subscriptionToMC` that holds the subscription object returned from the `sforce.one.subscribe()` method.

The `subscribeMC()` method checks whether the subscription object is empty. If it is, then it calls the `sforce.one.subscribe()` method. `sforce.one.subscribe()` has two parameters, the subscribing message channel, `onMCPublished()`, and the method that processes the message output.

By default, communication over a message channel can occur only between components in an active navigation tab, an active navigation item, or a utility item. Utility items are always active. A navigation tab or item is active when it’s selected. Navigation tabs and items include:

-   Standard navigation tabs
-   Console navigation workspace tabs
-   Console navigations subtabs
-   Console navigation items

To receive messages on a message channel from anywhere in the application, use the `sforce.one.subscribe()` method's optional fourth parameter, `subscriberOptions`. Set `scope` in the `subscriberOptions` to the value `"APPLICATION"`.

```apex
sforce.one.subscribe(messageChannel, listener, {scope: "APPLICATION"});
```

The `unsubscribeMC()` method checks whether there is a subscription object. If so, it calls `sforce.one.unsubscribe()`, and passes in the `subscriptionToMC` object. Then, it clears the `subscriptionToMC` object.

The `onMCPublished()` method converts the message payload from a JSON object to a string. It then displays the message in the `textarea` with the ID `MCMessageTextArea`.

```nolang
<apex:page >
    <div>
        <p>Subscribe to SampleMessageChannel </p>
        <button onclick="subscribeMC()">Subscribe</button>
        <p>Unsubscribe from subscription</p>
        <button onclick="unsubscribeMC()">Unsubscribe</button>
        <br/>
        <br/>
        <p>Received message:</p>
    <textarea id="MCMessageTextArea" rows="10" style="disabled:true;resize:none;width:100%;"/>
    </div>

    <script>
        // Load the MessageChannel token in a variable
        var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";
        var subscriptionToMC;

        function onMCPublished(message) {
            var textArea = document.querySelector("#MCMessageTextArea");
            textArea.innerHTML = message ? JSON.stringify(message, null, '\t') : 'no message payload';
        }

        function subscribeMC() {
            if (!subscriptionToMC) {
                subscriptionToMC = sforce.one.subscribe(SAMPLEMC, onMCPublished);
            }
        }

        function unsubscribeMC() {
            if (subscriptionToMC) {
                sforce.one.unsubscribe(subscriptionToMC);
                subscriptionToMC = null;
            }
        }
    </script>

</apex:page>

```

