I am unable to subscribe to events using emp API.

Taking a look at emp api here I have the following:


    <lightning-card title="StreamingPoc1 Example" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
                label="Channel Name"
            <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>


import { LightningElement } from 'lwc';
import {
} from 'lightning/empApi';

export default class StreamingPoc1 extends LightningElement {
    channelName = '/event/LoginEventStream';
    isSubscribeDisabled = false;
    isUnsubscribeDisabled = !this.isSubscribeDisabled;

    subscription = {};

    // Tracks changes to channelName text field
    handleChannelName(event) {
        console.log('I am in [handle Channel Name]');
        this.channelName = event.target.value;

    // Initializes the component
    connectedCallback() {
        // Register error listener

    // Handles subscribe button click
    handleSubscribe() {
        console.log('I am in [handle Subscribe]');
        // 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
        console.log('About to subscribe.....');
        // Invoke subscribe method of empApi. Pass reference to messageCallback
        subscribe(this.channelName, -1, messageCallback).then((response) => {
            // Response contains the subscription information on subscribe call
                'Subscription request sent to: ',
            this.subscription = response;
        console.log('After subscribe.....');

    // Handles unsubscribe button click
    handleUnsubscribe() {

        // 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


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

Lightning app (in developer console) that uses the component above

<aura:application >

When I hit the subscribe button, all I see in console is:

About to subscribe.....
After subscribe.....

And it doesn't seem to go in the subscribe block.

What am i doing wrong? I saw Unable to subscribe to platform event using emp api but that seems like a different issue.


The problem here is that you are putting your LWC inside an Aura App. Your subscription is not working because of that. If you use your component outside the Aura Application as a standalone component in a page, for instance, it should work perfectly. I've checked it in my org.

  • Even after subscribing I am not seeing notifications when someone logs in.
    – jini
    Oct 26 '21 at 13:32
  • 1
    Does your org fulfil the following requirements? Accessing this object requires either the Salesforce Shield or Salesforce Event Monitoring add-on subscription and the View Real-Time Event Monitoring Data user permission. (developer.salesforce.com/docs/atlas.en-us.platform_events.meta/…) If your org doesn't cover those requirements, that's probably the reason why you are not getting any events. I would suggest creating your own Platform Event and subscribing to that just check if the problem is the component or the channel. Oct 26 '21 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.