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:
- Authenticate (OAuth access token) and call the CometD endpoint
/cometd/<version>/. /meta/handshake, then/meta/subscribewith the channel name; then repeated/meta/connectlong-poll requests that the server holds open for about two minutes.- When an event occurs the server answers the pending connect with the message and the client reconnects immediately.
- 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.