Newer Version Available

This content describes an older version of this product. View Latest

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 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 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".
1sforce.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.

1<apex:page >
2    <div>
3        <p>Subscribe to SampleMessageChannel </p>
4        <button onclick="subscribeMC()">Subscribe</button>
5        <p>Unsubscribe from subscription</p>
6        <button onclick="unsubscribeMC()">Unsubscribe</button>
7        <br/>
8        <br/>
9        <p>Received message:</p>
10    <textarea id="MCMessageTextArea" rows="10" style="disabled:true;resize:none;width:100%;"/>
11    </div>
12
13    <script>
14        // Load the MessageChannel token in a variable
15        var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";
16        var subscriptionToMC;
17
18        function onMCPublished(message) {
19            var textArea = document.querySelector("#MCMessageTextArea");
20            textArea.innerHTML = message ? JSON.stringify(message, null, '\t') : 'no message payload';
21        }
22
23        function subscribeMC() {
24            if (!subscriptionToMC) {
25                subscriptionToMC = sforce.one.subscribe(SAMPLEMC, onMCPublished);
26            }
27        }
28
29        function unsubscribeMC() {
30            if (subscriptionToMC) {
31                sforce.one.unsubscribe(subscriptionToMC);
32                subscriptionToMC = null;
33            }
34        }
35    </script>
36
37</apex:page>