Insights

Real-Time Platform Events in Experience Cloud

January 7, 20266 minute readExperience CloudPlatform EventsCometDLightning Web Components
Real-Time Platform Events in Experience Cloud

Real-Time Platform Events in Experience Cloud

If you’ve ever tried to consume Salesforce Platform Events from a Lightning Web Component (LWC) running in Experience Cloud, you’ve probably hit a hard wall.

The standard solution — lightning/empApi — simply does not work reliably in Experience Cloud. And when you’re building partner portals, customer portals, or B2B experiences, that limitation becomes painful very quickly.

This repository exists to solve exactly that problem.


The Problem with EMP API in Experience Cloud

In the standard Salesforce UI, subscribing to Platform Events is straightforward:

    import {subscribe} from 'lightning/empApi';

However, Experience Cloud LWCs run outside the standard Salesforce UI context. As a result:

  • lightning/empApi is unsupported or unreliable
  • Platform Events cannot be consumed in real time
  • UI refreshes fall back to polling or manual reloads
  • Real-time portal experiences become unnecessarily complex

There is no officially supported EMP API alternative for Experience Cloud. (yet)


Why CometD?

Under the hood, Salesforce’s EMP API is built on top of CometD, which implements the Bayeux protocol for publish/subscribe messaging.

Instead of relying on an unsupported abstraction, this repository uses:

  • The CometD JavaScript client
  • Loaded as a static resource
  • Authenticated using the running portal user session
  • Connected directly to Salesforce’s /cometd/{apiVersion} endpoint

In short:

We bypass EMP API and talk to the same underlying infrastructure it uses.

What This Repository Provides

This repository delivers a production-ready, reusable CometD client for Experience Cloud LWCs:

  • Platform Event subscriptions (/event/*)
  • Experience Cloud compatibility
  • Authenticated user context via Apex
  • Clean lifecycle management (subscribe / unsubscribe / disconnect)
  • Reconnect and retry foundations
  • Clear separation between UI logic and infrastructure code

The client is designed to be shared across LWCs, not copied per component.


What This Repo Does Not Try to Do

This is intentionally not a general-purpose real-time framework.

It does not:

  • Support Guest users
  • Replace EMP API everywhere
  • Hide Salesforce security constraints
  • Abstract away session-based authentication

These constraints are documented explicitly, because real-time infrastructure should never feel “magical”.


When Should You Use This?

This approach is a good fit when:

  • You are building Experience Cloud portals
  • You need real-time updates driven by Platform Events
  • Polling is not acceptable nor ideal
  • EMP API is unavailable
  • Authenticated users are acceptable

Repository Structure (High Level)

  • Static Resource: CometD UMD bundle
  • Apex Controller: Provides authenticated context
  • LWC Module: Handles CometD lifecycle and subscriptions
  • README: Architecture, security, lifecycle, and troubleshooting

The codebase is intentionally small, focused, and documented as infrastructure.


GitHub Repository

You can find the full implementation, documentation, and usage examples here:

GitHub: https://github.com/iahmadezzat/sf-cometd-client


Final Thoughts

Experience Cloud is often treated as a second-class citizen when it comes to real-time capabilities. This repository exists because real-time UX shouldn't disappear the moment you leave the standard Salesforce UI.

If you’re building serious portal experiences, you’ll eventually need something like this — implemented cleanly, documented properly, and explicit about its trade-offs.

Feel free to explore, adapt, improve, and share it back with community.