Overview
The Salesforce Streaming API enables real-time push notifications from Salesforce to external clients when data changes or specific events occur. Unlike REST API polling, Streaming API uses a long-lived, low-latency connection (based on the Bayeux protocol / CometD) to push updates as they happen — reducing latency, lowering API usage, and making integrations more responsive.
When to use the Streaming API
Use Streaming API for real-time UI updates, synchronization between systems, monitoring record changes, or event-driven workflows where near-instant visibility is required without frequent polling.
Mechanisms (Channel Types) of the Streaming API
1) PushTopic Streaming
PushTopic is the original streaming mechanism. You define a PushTopic (SOQL-based) that specifies which objects and fields to watch and which DML operations (create/update/delete/undelete) should trigger notifications. When a record matching the PushTopic’s query changes, a notification is published to the /topic/
Key points:
- Declarative selector via SOQL.
- Best for monitoring a subset of records matching criteria.
- Retains a replay ID for clients to recover missed messages (if supported).
2) Generic Streaming
Generic Streaming provides a lightweight, custom channel (/event/yourChannel) that publishers (via API) can push arbitrary messages to. It’s not tied to object DML and is useful when you want to publish custom notifications from Apex, external systems or flows.
Key points:
- Flexible payloads defined by publisher.
- Good for custom application events not directly tied to object changes.
3) Platform Events
Platform Events are part of Salesforce’s event-driven architecture (highly reliable, scalable) and use a publish-subscribe model. You define a Platform Event schema (custom event object) and then publish events via Apex, REST API, or Process Builder/Flow. Subscribers (CometD clients, Apex triggers, or external middleware) receive events in real time.
Key points:
- Durable delivery and replay capability (replay IDs).
- Integrates with Apex triggers, Flows, and external systems.
- Better suited for complex, enterprise-grade eventing than PushTopic.
4) Change Data Capture (CDC)
Change Data Capture publishes change events for Salesforce records and fields automatically. CDC is the recommended way to capture create/update/delete/undelete events for standard and custom objects. It publishes a standardized change event stream (payload includes change metadata and before/after values).
Key points:
- Automatically publishes changes without writing triggers.
- Payload contains changeType, record IDs, changed fields, and replayId.
- Excellent for data replication, caches, and analytics pipelines.
Transport & Protocol
All streaming mechanisms use the Bayeux protocol and CometD client libraries. Clients establish a CometD handshake over HTTPS to Salesforce’s /cometd/
Authentication & Replay
Clients authenticate using OAuth access tokens (same as other Salesforce APIs). Most streaming channels support replayId — a numeric cursor that lets a client request events starting from a specific point to avoid data loss after disconnects.
Limits & Best Practices
- Monitor event volume and platform limits (events per 24-hour period, concurrent clients, and message size).
- Use replayId where possible to guarantee at-least-once delivery.
- Filter data using PushTopic queries or CDC object selection to reduce noise.
- Prefer CDC or Platform Events for enterprise integrations; use PushTopic for simple record-specific subscriptions.
- Design idempotent subscribers — events may be delivered more than once.
Example: Simple CometD JavaScript subscription (PushTopic)
// Include a CometD library (e.g., cometd.js) and then:
var cometd = new org.cometd.CometD();
cometd.configure({
url: 'https://yourInstance.salesforce.com/cometd/56.0',
requestHeaders: { Authorization: 'Bearer ' + accessToken },
appendMessageTypeToURL: false
});
cometd.handshake(function(handshakeReply) {
if (handshakeReply.successful) {
// Subscribe to a PushTopic channel named AccountUpdates
cometd.subscribe('/topic/AccountUpdates', function(message) {
console.log('Received event:', message);
});
} else {
console.error('Handshake failed', handshakeReply);
}
});
Summary
The Salesforce Streaming API family enables efficient, real-time integrations using PushTopic, Generic Streaming, Platform Events, and Change Data Capture. Choose PushTopic for SOQL-based subscriptions, Generic for custom lightweight messages, Platform Events for structured event-driven architectures, and CDC for automatic record-change replication. Implement replay logic, idempotent handlers, and filtering to build robust real-time integrations.








Leave a Reply