3

I am new to LWC and trying to implement empApi for publishing and subscribing platform event from LWC.

Salesforce provided an example on:

https://developer.salesforce.com/docs/component-library/bundle/lightning-emp-api/documentation

But I am not getting any response

Template Code:

<template>
    <lightning-card title="EmpApi Example" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
            <p>Use the buttons below to subscribe and unsubscribe to a streaming channel!</p>
            <lightning-input label="Channel Name" value={channelName}
                onchange={handleChannelName}></lightning-input>
            <lightning-button variant="success" label="Subscribe" title="Subscribe"
                onclick={handleSubscribe} disabled={isSubscribeDisabled}
                class="slds-m-left_x-small"></lightning-button>
            <lightning-button variant="destructive" label="Unsubscribe" title="Unsubscribe"
                onclick={handleUnsubscribe} disabled={isUnsubscribeDisabled}
                class="slds-m-left_x-small"></lightning-button>
        </div>
    </lightning-card>
</template>

JS Code:

import { LightningElement, track } from 'lwc';
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';

export default class EmpApiLWC extends LightningElement {
    @track channelName = '/event/Test__e';
    @track isSubscribeDisabled = false;
    @track isUnsubscribeDisabled = !this.isSubscribeDisabled;

    subscription = {};

    // Tracks changes to channelName text field
    handleChannelName(event) {
        this.channelName = event.target.value;
    }

    // Handles subscribe button click
    handleSubscribe() {
        // Callback invoked whenever a new event message is received
        const messageCallback = function(response) {
            console.log('New message received : ', JSON.stringify(response));
            // Response contains the payload of the new message received
        };

        // Invoke subscribe method of empApi. Pass reference to messageCallback
        subscribe(this.channelName, -1, messageCallback).then(response => {
            // Response contains the subscription information on successful subscribe call
            console.log('Successfully subscribed to : ', JSON.stringify(response.channel));
            this.subscription = response;
            this.toggleSubscribeButton(true);
        });
    }

    // Handles unsubscribe button click
    handleUnsubscribe() {
        this.toggleSubscribeButton(false);

        // Invoke unsubscribe method of empApi
        unsubscribe(this.subscription, response => {
            console.log('unsubscribe() response: ', JSON.stringify(response));
            // Response is true for successful unsubscribe
        });
    }

    toggleSubscribeButton(enableSubscribe) {
        this.isSubscribeDisabled = enableSubscribe;
        this.isUnsubscribeDisabled = !enableSubscribe;
    }

    registerErrorListener() {
        // Invoke onError empApi method
        onError(error => {
            console.log('Received error from server: ', JSON.stringify(error));
            // Error contains the server-side error
        });
    }
}
1
  • 1
    Can you add relevant code snippets? Commented Oct 10, 2019 at 13:33

1 Answer 1

3

I got the solution. A small change was required in documentation only. I have posted the details in salesforce forum check here https://developer.salesforce.com/forums/ForumsMain?id=9062I000000Xm9eQAC


Quoting answer from developer forum:

In the question it self I proposed the correct way to subscribe the event in lwc which by making small change in messageCallback method.

const messageCallback = (response) => {
    console.log('New message received : ', JSON.stringify(response));
    this.payload = JSON.stringify(response);
    console.log('this.payload: ' + this.payload);
    // Response contains the payload of the new message received
};
1
  • Good catch! Can you explain > "We have to use arrow function as it does not have its own scope."
    – Atlas Can
    Commented Oct 29, 2019 at 5:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .