Salesforce Messaging for In-App and Web Conversation End Event

Salesforce Messaging for In-App and Web Conversation End Event

Salesforce messageChannel “lightning__conversationEnded” can be used to listen to the Salesforce Messaging for In-App and Web Conversation End Event.

Sample Lightning Web Component:

HTML:

<template>    
</template>

JavaScript:

import { LightningElement, wire } from 'lwc';
import { MessageContext, subscribe, unsubscribe, APPLICATION_SCOPE } from "lightning/messageService";
import conversationEndedChannel from "@salesforce/messageChannel/lightning__conversationEnded";

export default class MessagingEndListener extends LightningElement {

    subscription = null;
    @wire( MessageContext ) 
    messageContext;

    subscribeToMessageChannel() {

        if ( !this.subscription ) {

            this.subscription = subscribe(
                this.messageContext,
                conversationEndedChannel,
                ( message ) => this.handleMessage( message ),
                { scope: APPLICATION_SCOPE }
            );

        }

    }

    unsubscribeToMessageChannel() {

        unsubscribe(this.subscription);
        this.subscription = null;

    }

    handleMessage( message ) {

        console.log(
            'Message received is',
            JSON.stringify(
                message
            )
        );
        
    }
    
    connectedCallback() {
        
        console.log(
            'Subscribing'
        );
        this.subscribeToMessageChannel();

    }

    disconnectedCallback() {
                
        console.log(
            'Unsubscribing'
        );
        this.unsubscribeToMessageChannel();

    }

}

js-meta.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>59.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

Leave a Reply