Yes! You heard me right you can use Lightning Message Service in Visualforce pages too. But there is a catch.

You can use LMS in Visualforce page only when the Visualforce page component is dragged and dropped on a record page.

There is a reason behind it. The JavaScript library is included in Visualforce only when it's embedded in Lightning like how I have mentioned above. You cannot use it in Salesforce Classic and you cannot use it when you try to iframe Visualforce page in an Aura Component.

Let's look at how to use LMS in Visualforce pages.

Here is the snippet!

<apex:page lightningStylesheets="true">
    <apex:includeLightning />

    <div id="lightning" />

    <button onclick="handlePublish()">

		//Get reference to the message channel
        const GINFO = "{!$MessageChannel.LeadDataMessageChannel__c}"

        function handlePublish() {
            const payLoad = {
                name: 'Teja'
            //publish the data to Message Channel
  , payLoad);

This is the Visualforce page that will subscribe to the message channel and starts receiving the data once it's pushed to message channel.

<apex:page lightningStylesheets="true">

    <button onclick="handleSubscribe()">

    <div class="receivedText">

    <button onclick="handleUnsubscribe()">
    <button onclick="clearData()">
        Clear Data

        //get reference to the message channel
        const GINFO = "{!$MessageChannel.LeadDataMessageChannel__c}";
        let subscription = null;

        //function to handle subscribe functionality
        function handleSubscribe() {
            //check if the user is already subscribed
            if (!subscription) {
                //business logic is refactored to handleMessage
                subscription =, handleMessage);


        //function to handle unsubscribe
        function handleUnsubscribe() {
            subscription = null;

        function handleMessage(message) {
            let receivedText = JSON.stringify(message, null, "\t");
            document.querySelector('.receivedText').innerHTML = receivedText;

        //function to clear the data
        function clearData() {
            document.querySelector('.receivedText').innerHTML = '';


Hope this helps!