# How can developers utilize Platform Events and LWC to power real-time dashboard data updates without refreshing?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Real-Time Dashboards, LWC Integration

Read it on the site: https://www.forcechallenges.com/interview-questions/how-can-developers-utilize-platform-events-and-lwc-to-power-real-time-dashboard-data-updates-without

## Answer

**Basics:** Standard dashboards and reports refresh on a schedule or manual action. For live operations screens (queue depth, order status), the server should push changes. Platform events are Salesforce's publish/subscribe messages and `empApi` lets a Lightning component listen to them.

**How it works:**
1. Define a platform event (`Metric_Update__e`) with fields such as `Metric_Name__c` and `Value__c`.
2. Publish from Apex (`EventBus.publish`), Flow ("Create Records" on the event object), or an external system through REST.
3. In the LWC, import `subscribe`, `unsubscribe`, `onError` from `lightning/empApi`, subscribe to `/event/Metric_Update__e` in `connectedCallback`, and update tracked state in the callback.
4. Unsubscribe in `disconnectedCallback`.
5. Users need Read access to the event object.

**Advanced / deep dive:** For record-change dashboards, Change Data Capture channels (`/data/AccountChangeEvent`) avoid custom publishing code. Push small deltas or just record IDs and then call `refreshApex` on a wired method so data respects sharing. Throttle UI updates (debounce) when events arrive rapidly. Event delivery is not exactly-once and the dashboard may reconnect and miss events, so on reconnect do a full refresh query. Streaming client allocations and daily event delivery limits depend on edition (verify). `empApi` is for Lightning Experience; support in Experience Cloud/LWR and mobile has restrictions, so check docs or fall back to polling.

**Code / config example:**
```javascript
import { LightningElement } from 'lwc';
import { subscribe, unsubscribe, onError } from 'lightning/empApi';

export default class LiveMetrics extends LightningElement {
    channel = '/event/Metric_Update__e';
    subscription = null;
    metrics = {};

    connectedCallback() {
        onError(err => console.error('empApi error', JSON.stringify(err)));
        subscribe(this.channel, -1, msg => {
            const p = msg.data.payload;
            this.metrics = { ...this.metrics, [p.Metric_Name__c]: p.Value__c };
        }).then(resp => { this.subscription = resp; });
    }
    disconnectedCallback() {
        if (this.subscription) unsubscribe(this.subscription, () => {});
    }
}
```

**Hidden gotchas & must-remember points:**
- Always unsubscribe, or listeners leak.
- Platform events ignore sharing; do not push sensitive data.
- Replay `-1` means only events after subscription; do an initial data load separately.
- There is no guaranteed delivery on a closed tab; refresh on load.
- `empApi` does not work in all contexts (e.g., certain Experience Cloud setups).
- Publishing counts against event publishing allocations.

**Best practice:** Load initial state with wire/Apex, then apply pushed deltas, and refresh fully on reconnect.

---
Source: ForceChallenges · last updated 2026-11-02
