You do not need EMP-Connector if you are using lwc. Salesforce has a lightning-emp-api
base component for it (https://developer.salesforce.com/docs/component-library/bundle/lightning:empApi).
The lightning/empApi module uses a shared CometD connection.
You can use the provided functions from the module
import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled } from 'lightning/empApi';
Update
You are right that for communities this component is not supported.I still suggest you give a try because i have it working at least for aura component in the lightning community builder using emp api. I have not tested the lwc one.
Also push topics might not be supported but platform events are supported in the lightning communities.
One can completely engineer the emp api component with a custom component because under the hood it uses cometd js library.
Here is a sample code to get you started
NOTE - I have used the 3.1.1 version from here. You can extract the archive and under the common folder just download the comted.js file for using in static resource.
I also uploaded the cometd.js file that i have used for testing. You can get it here
Here is the sample code to get started
<template>
<div>Streaming Component</div>
</template>
The JS controller is
import { LightningElement, wire, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import cometdlwc from "@salesforce/resourceUrl/cometd";
import getSessionId from '@salesforce/apex/SessionUtil.getSessionId';
export default class Cometdlwc extends LightningElement {
libInitialized = false;
@track sessionId;
@track error;
@wire(getSessionId)
wiredSessionId({ error, data }) {
if (data) {
console.log(data);
this.sessionId = data;
this.error = undefined;
loadScript(this, cometdlwc)
.then(() => {
this.initializecometd()
});
} else if (error) {
console.log(error);
this.error = error;
this.sessionId = undefined;
}
}
initializecometd() {
if (this.libInitialized) {
return;
}
this.libInitialized = true;
//inintializing cometD object/class
var cometdlib = new window.org.cometd.CometD();
//Calling configure method of cometD class, to setup authentication which will be used in handshaking
cometdlib.configure({
url: window.location.protocol + '//' + window.location.hostname + '/cometd/47.0/',
requestHeaders: { Authorization: 'OAuth ' + this.sessionId},
appendMessageTypeToURL : false,
logLevel: 'debug'
});
cometdlib.websocketEnabled = false;
cometdlib.handshake(function(status) {
if (status.successful) {
// Successfully connected to the server.
// Now it is possible to subscribe or send messages
console.log('Successfully connected to server');
cometdlib.subscribe('/event/PlatformEvent__e', function (message) {
console.log('subscribed to message!'+ message);
});
} else {
/// Cannot handshake with the server, alert user.
console.error('Error in handshaking: ' + JSON.stringify(status));
}
});
}
}
It requires a simple apex controller to get sessionId
public with sharing class SessionUtil {
@AuraEnabled(cacheable=true)
public static String getSessionId() {
return UserInfo.getSessionId();
}
}