Salesforce Decode
Salesforcedecode
Back to questions
LWCAdvancedwirereal-timeerror-handling

Subscribe to Platform Events in LWC with empApi reconnect handling

Real World Scenario

Operations dashboard empApi subscription silently dies after laptop network reconnect. Case counts stale until page reload.

Expected Answer

• empApi onError handler logging and user-visible reconnect banner • navigator online event triggers resubscribe with replayId if stored • Unsubscribe clean before resubscribe preventing duplicate handlers • Fallback polling interval when empApi unavailable in Experience guest • disconnectedCallback unsubscribe mandatory • Merge events into @track state immutably • Test empApi mock subscribe error callback in Jest

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Direct answer: empApi onError handler logging and user-visible reconnect banner Also consider: navigator online event triggers resubscribe with replayId if stored In practice: Unsubscribe clean before resubscribe preventing duplicate handlers Optimize for scale and operational observability.

Architect Perspective

empApi connections are fragile on mobile networks—reconnect strategy is production requirement.