+ Start a Discussion
Raksha JhaRaksha Jha 

LMS with LWC - Datatable

How to send notification via LMS on change of data in LWC - Datatable?
I have created a leaderboard which display the scores of winning player through LWC- Datatable. i have to publish message through LMS whenever winning player change.
This is Apex class:
public with sharing class SquadLeaderBoardHandler {
    public static List<Account> getUpdatedScore() 
        Contact con = [SELECT AccountId FROM Contact WHERE Email = :UserInfo.getUserEmail()];

        Account objAccount = new Account();
        objAccount = [SELECT Name, Squad__r.Name, Id FROM Account WHERE Id = :con.AccountId];
        //WHERE Id = :objUser.Id

        return [SELECT Name, Weekly_Arete_Number__c, Squad__r.Name FROM Account 
        WHERE Squad__r.Name = :objAccount.Squad__r.Name ORDER BY Weekly_Arete_Number__c DESC LIMIT 3];
	<lightning-card title={squadName} onchange={publishMessage} icon-name="utility:activity">		
			<div if:true={accounts}>
				<lightning-datatable data={accounts}

import { LightningElement, wire, track } from 'lwc';
import getUpdatedScore from '@salesforce/apex/SquadLeaderBoardHandler.getUpdatedScore';
import LBMC from '@salesforce/messageChannel/LeaderBoardMessageChannel__c';
import { MessageContext, publish } from 'lightning/messageService';

// datatable columns
const COLUMNS = [
    { label: 'Hero Name', fieldName: 'Name', hideDefaultActions: true, type: 'text' },
    { label: 'Hero Score', fieldName: 'Weekly_Arete_Number__c', hideDefaultActions: true, type: 'Number' },

export default class SquadLeaderboard extends LightningElement {
    @track squadName;
    @track accounts;
    @track columns = COLUMNS;
    @track error;

    wiredupdatedScore({ error, data }) {
        if (data) {
            this.accounts = data;
            this.error = undefined;
            this.squadName = 'Squad Leaderboard for: ' + this.accounts[0].Squad__r.Name;

        } else if (error) {
            this.error = error;
            this.accounts = undefined;

    publishMessage(event) {
        this.squadName = event.target.value;
        console.log('SquadName: ' + this.squadName);
        const message = { slbData: { value: this.squadName } };
        publish(this.context, LBMC, message);


message channel
<?xml version="1.0" encoding="UTF-8" ?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
    <description>Message Channel to pass data</description>
        <description>This is the field to pass SquadLeaderBoard Data</description>
			<description>This is the field to pass CohortLeaderBoard Data</description>
			<description>This is the field to pass TeamLeaderBoard Data</description>


LMS is for passing data between the components. You can try using PushTopic and emApi.
Sample Code: https://www.infallibletechie.com/2020/03/subscribe-and-show-pushtopic-events.html
Raksha JhaRaksha Jha
Thank you for your response. I have to publish the winner from leaderboard so that other components subscribe and a notification will display with winner name using LMS.