# How does the Streaming API utilize the CometD protocol to deliver real-time notifications to external clients?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Streaming API, CometD Protocol

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-the-streaming-api-utilize-the-cometd-protocol-to-deliver-real-time-notifications-to-external-clients

## Answer

**Basics:** Instead of polling for changes, the client keeps a long-lived connection and Salesforce pushes updates. Channel types: PushTopic (`/topic/AccountUpdates`, driven by a SOQL query), Generic Streaming (`/u/MyChannel`, custom payloads), Platform Events (`/event/Name__e`) and Change Data Capture (`/data/AccountChangeEvent`). Salesforce now recommends Pub/Sub API (gRPC) for new external subscribers, but CometD remains supported.

**How it works:**
1. Authenticate (OAuth access token) and call the CometD endpoint `/cometd/<version>/`.
2. `/meta/handshake`, then `/meta/subscribe` with the channel name; then repeated `/meta/connect` long-poll requests that the server holds open for about two minutes.
3. When an event occurs the server answers the pending connect with the message and the client reconnects immediately.
4. For PushTopic, create the topic record: it contains the query and which operations (create, update, delete, undelete) and field changes (`NotifyForFields`: Referenced, Select, Where, All) generate notifications.

**Advanced / deep dive:** Use a maintained CometD library (for example the Java or JavaScript CometD client, or `lightning/empApi` inside LWC) because handling reconnection, advice and handshake renewal manually is error-prone. Subscribers can ask for replay using the ReplayId extension. PushTopic queries are a restricted SOQL subset (for example no aggregates, LIMIT or ORDER BY; verify details), and PushTopics are limited per org and in concurrent clients and event deliveries per 24 hours; verify exact numbers in the Streaming API limits docs. Security: the subscriber needs access to the object and fields, and record-level sharing is checked, while platform events use object permissions. Compared with CDC, PushTopics need manual maintenance and have fewer guarantees; CDC is generally preferred for record-change data.

**Code / config example:**
```apex
PushTopic pt = new PushTopic(
    Name = 'AccountUpdates', ApiVersion = 62.0,
    Query = 'SELECT Id, Name, Rating FROM Account',
    NotifyForOperationCreate = true, NotifyForOperationUpdate = true,
    NotifyForFields = 'Referenced');
insert pt;
```
```json
{"channel":"/meta/subscribe","subscription":"/topic/AccountUpdates","clientId":"<id>"}
```

**Hidden gotchas & must-remember points:**
- The client must keep reconnecting; dropping the connect loop silently stops messages.
- PushTopic and Generic Streaming events are retained for 24 hours, platform events and CDC for 72 hours.
- Streaming is best-effort at the connection level; use ReplayId to recover.
- Too many concurrent subscribers or events hits allocation limits.
- Session timeout or token expiry requires re-handshake with a fresh token.

**Best practice:** Use platform events or CDC channels with Pub/Sub API or a maintained CometD client, and store the last ReplayId.

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