I want to subscribe to events in a community using the CometD library. Unfortunately when loading the script I get following error: "Error: Cannot read properties of undefined (reading 'cometd')"

This is my code:

import { LightningElement, api, wire, track } from 'lwc';
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';
import triggerSomeEventBoy from '@salesforce/apex/AccountController.triggerSomeEventBoy';
import getAllActiveAccounts from '@salesforce/apex/AccountController.getAllActiveAccounts';
import getSessionId from '@salesforce/apex/AccountController.getSessionId';
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from '@salesforce/resourceUrl/CometD';

export default class GerkesComponent extends LightningElement {
    libInitialized = false;
    @track sessionId;
    @track error;

    sessionId = "";
    wiredSessionId(result) {
        var error = result.error;
        var data = result.data;
        if(data) {
            console.log('sesssion id: ' + data);
            this.sessionId = data;
            this.error = undefined;
            loadScript(this, cometdlwc).then(() => {
                console.log('script loaded yippie!');
            }).catch((error) => {
                console.log('found error:');
        } else if (error) {
            this.error = error;
            this.sessionId = undefined;

Does anyone know why this error occurs and how to solve it?

  • Hi - just want to make sure I understand the use case right - are you going to subscribe to a CometD stream that's not from Salesforce Streaming API? If it's just Streaming API, emApi alone is enough
    – Bing
    Oct 24, 2021 at 21:27
  • I want to subscribe to Salesforce Platform Events in a Community. EmpApi unfortunately does not work in Communities.
    – Gerke
    Oct 25, 2021 at 7:11


You must log in to answer this question.