ForceChallenges
ProblemsDailyNotesInterview
Sign in

Practice

All problemsSOQL practiceApex trigger challengesDaily challengeOpportunityTrigger.oldMapBefore UpdateBulkificationContactValidationGROUP BYSemi-JoinAccountBefore Insert

Interview questions

All questionsAdminAdministrationApexApex TriggerAsync ApexDevelopmentField ServiceField ServiceIntegrationLwc

By company

DeloitteSalesforceAccentureInfosysPwCCognizant

Learn

Study notes

ForceChallenges

AboutContact usPrivacy PolicyTerms of ServiceFor AI assistants (llms.txt)
© 2026 ForceChallenges · Salesforce SOQL & Apex trigger practice
Integration questions19 of 100
IntegrationMedium 2 min read

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

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.

IntegrationReal-Time DashboardsLWC Integration
Practice SOQL & Apex
PreviousHow do Lightning Web Components integrate with external APIs using native fetch and Apex controllers?Next How do streaming client applications handle unexpected network drops and implement automatic reconnection logic?

More Integration questions

  • How do PushTopics combine SOQL query criteria with real-time streaming notifications in Salesforce?
  • How do webhook security mechanisms verify the authenticity of incoming HTTP notifications from external systems?
  • How do federated identity and SAML single sign-on streamline authentication across enterprise integration landscapes?
  • How do CORS settings permit authorized web applications to perform cross-origin REST requests against Salesforce?
  • How do rate limiting and throttling mechanisms protect Salesforce endpoints from excessive traffic spikes?