Sub topic 13
Subscribing from external systems with Pub/Sub API
Apex triggers, flows, and Lightning components all run inside Salesforce. An external system, such as a data warehouse loader or a middleware service, needs another way to receive events. Pub/Sub API is that way. This chapter assumes you know how replay IDs and the retention window work, and that you have a platform event such as Order_Shipped__e defined.
What Pub/Sub API is
Pub/Sub API is a gRPC API. gRPC is a remote procedure call framework that runs over HTTP/2 and sends binary messages. The client opens one long-lived connection to Salesforce and receives events as they are published. The client does not poll.
Pub/Sub API can also publish events, but this chapter covers subscribing only.
How a subscription works
The client calls the Subscribe method, which is a bidirectional stream. The client sends fetch requests, and the server sends back batches of events.
The steps are:
- Authenticate. Get an access token with an OAuth flow. Each call carries the access token, the instance URL, and the org ID as gRPC metadata.
- Get the topic and schema. A topic name for a platform event is
/event/plus the API name, for example/event/Order_Shipped__e. Each event carries a schema ID. CallGetSchemawith that ID to get the Avro schema. - Subscribe. Send a
FetchRequestwith the topic name, a replay preset, and the number of events you want. - Decode. Each event payload is binary Avro. Decode it with the schema from step 2.
Replay and flow control
The replay preset says where to start. It uses the replay ID concepts from the earlier chapter.
| Replay preset | Behavior |
|---|---|
| `LATEST` | Only events published after the subscription starts. |
| `EARLIEST` | Every event still inside the retention window. |
| `CUSTOM` | Events after the replay ID you supply. |
Store the replay ID of each event you finish processing. After a crash or a deploy, restart with CUSTOM and that ID. If the stored ID is older than the retention window, the events between it and the oldest retained event are gone. Your client must detect that case and recover another way, for example by querying the source records.
Pub/Sub API uses pull-based flow control. The server sends only as many events as the num_requested value in your fetch requests. When you have processed a batch, send another FetchRequest to ask for more. A client that never requests more simply stops receiving events.
The server also sends keepalive responses with no events at intervals. These carry the latest replay ID, so you can advance your stored checkpoint even when no events match.
Common errors
- Decoding with a stale schema. Always fetch the schema by the ID on the event. Cache it by ID, not by topic. A new field on the event produces a new schema ID.
- Losing the replay ID. If you store it before processing finishes, a crash skips an event. Store it after processing.
- Letting the token expire. A long-lived stream outlasts a short session. Refresh the token and reconnect, or follow the authentication refresh handling in the API documentation.
- Assuming exactly-once delivery. Make processing idempotent, because a restart from a stored replay ID can redeliver events.
Exercise
Your external subscriber crashes and restarts after 10 hours. Which replay preset do you use, and what do you need to have saved?
Show answer
Use CUSTOM with the replay ID of the last event you fully processed. Save that ID after processing each event or batch. Ten hours is inside the retention window, so Salesforce can replay the missed events. Because redelivery is possible, processing should be idempotent.