Sub topic 1
What the Pub/Sub API is and when to use it
Prerequisites
This chapter assumes you have the following:
- Basic Salesforce concepts: orgs, objects, and connected apps or external client apps.
- Comfort reading and writing Python.
- Familiarity with OAuth 2.0 access tokens.
- A general idea of publish/subscribe messaging.
You do not need any gRPC experience yet. Later chapters cover it.
What the API is
The Salesforce Pub/Sub API is a gRPC interface for exchanging events with an org. gRPC is a remote procedure call framework that runs over HTTP/2. A client connects to api.pubsub.salesforce.com:7443 and calls methods on a single service.
The API lets you receive three kinds of events:
- Platform events, which you define and publish yourself.
- Change Data Capture (CDC) events, which report record changes.
- Real-time event monitoring events, which report user and security activity.
You can also publish platform events through the same service. This tutorial focuses on subscribing.
The working model
A client opens one long-lived HTTP/2 connection. It asks for events from a named topic, and the server pushes events back as they occur. Each event carries a binary Avro payload, a schema ID, and a replay ID. The replay ID marks the event's position in the topic, so a client can resume from it later.
The service exposes a small set of RPCs:
| RPC | Purpose |
|---|---|
| GetTopic | Returns topic details, including whether you can subscribe and which schema ID it uses |
| GetSchema | Returns the Avro schema for a schema ID |
| Subscribe | Opens a bidirectional stream for receiving events |
| Publish and PublishStream | Send platform events to the org |
| ManagedSubscribe | Lets the server track your replay position |
How it differs from other streaming options
Older Salesforce streaming used the Streaming API, which is built on CometD. CometD is a long-polling protocol that delivers JSON messages. The Pub/Sub API replaces that model with binary Avro payloads and explicit flow control. It also has one API for publishing and subscribing.
| Option | Transport | Payload | Typical use |
|---|---|---|---|
| Pub/Sub API | gRPC over HTTP/2 | Avro binary | External services that need high-throughput, resumable delivery |
| Streaming API (CometD) | Long polling | JSON | Existing integrations and browser-based clients |
| Apex triggers and flows | Inside Salesforce | Native objects | Logic that stays within the org |
When to choose it
Choose the Pub/Sub API when:
- An external system outside Salesforce must react to events.
- You need to resume after a disconnect without losing events. Replay IDs make this possible.
- You want to limit how fast events arrive. The
num_requestedfield in a fetch request does this. - You want one client for platform events, CDC, and monitoring events.
Prefer an Apex trigger or a flow when the reaction belongs inside Salesforce. They need no external client or connection handling.
Common errors at this stage
- Treating the API like REST. The connection is a stream that stays open, not a request that returns once.
- Expecting JSON. Payloads are Avro bytes and cannot be read without the schema.
- Forgetting that the server only sends events you have requested. A subscriber that never asks for more events will appear to stall.
- Ignoring replay IDs. Without a stored replay ID, a restart can miss events or reprocess them.
Exercise
Your service must react to Account changes from outside Salesforce and must not lose events when it restarts. Which API do you choose, and which field in each event lets you resume afterward?
Show answer
Use the Pub/Sub API and subscribe to the Account Change Data Capture topic. Store each event's replay ID and pass it when you reconnect.