Sub topic 3
gRPC and Bidirectional Streaming in Brief
This chapter assumes you can read Python, know what an OAuth access token is, and have a general idea of publish/subscribe messaging. You don't need prior gRPC experience. Here you will learn the parts of gRPC that the Salesforce Pub/Sub API relies on, so the later chapters on stubs, authentication, and Subscribe make sense.
What gRPC is
gRPC is a remote procedure call framework. You call a function on a client object, and the library sends the call to a server and returns the result. It runs over HTTP/2 and encodes messages with Protocol Buffers (protobuf), a compact binary format. The Pub/Sub API is served at api.pubsub.salesforce.com:7443 over TLS.
Two properties matter here:
- A contract file defines the API. A
.protofile lists the services, their methods, and the message types. Both sides generate code from it. - HTTP/2 allows many streams on one connection. A call can stay open and carry many messages in either direction.
The four call types
gRPC methods come in four shapes. The Pub/Sub API uses three of them.
| Call type | Client sends | Server sends | Pub/Sub API example |
|---|---|---|---|
| Unary | One message | One message | `GetTopic`, `GetSchema` |
| Server streaming | One message | Many messages | Not used by this API |
| Client streaming | Many messages | One message | Not used by this API |
| Bidirectional streaming | Many messages | Many messages | `Subscribe`, `PublishStream` |
GetTopic and GetSchema behave like ordinary request and response calls. Subscribe is different. The client opens one long-lived call and keeps sending small requests on it, and the server keeps sending batches of events back.
Why `Subscribe` is bidirectional
You might expect a subscription to be server streaming: ask once, then receive events. The Pub/Sub API uses bidirectional streaming because the client also needs to talk during the stream. Each FetchRequest you send says how many more events you are ready to receive. This is how flow control works, and a later chapter covers it in detail.
How streaming looks in Python
In Python, a bidirectional call takes an iterator of request messages and returns an iterator of responses. The library reads from your request iterator in the background while you loop over the responses.
The example below shows the shape with no Salesforce types. It uses a generator that blocks on a queue, which is the same pattern the Subscribe example in a later chapter uses.
import queue
outgoing = queue.Queue()
def request_iterator():
# The gRPC library pulls from this generator whenever it can send.
while True:
yield outgoing.get()
# responses = stub.Subscribe(request_iterator(), metadata=metadata)
# for response in responses:
# handle(response)
# outgoing.put(next_request)Because outgoing.get() blocks, the generator sends nothing until you put a request on the queue. That is how you send a new FetchRequest after processing a batch.
Metadata and the stream lifetime
gRPC sends key-value pairs called metadata along with each call, similar to HTTP headers. The Pub/Sub API reads your credentials from metadata, not from the message body. You set it once when you start the call, and it applies to the whole stream. The next chapters cover which keys are required.
A stream can end in several ways:
- The server closes it, for example after an error or a deployment.
- The network drops.
- Your code stops iterating or cancels the call.
A stream is not a durable subscription. When it ends, you open a new one and tell the server where to resume. That position is the replay ID, covered later.
Common errors at this stage
- Blocking the response loop. Slow event handling delays reading and can make your processing lag behind the server.
- A request generator that ends. If the generator returns, the client side of the stream closes. For
Subscribe, keep it open for as long as you want events. - Treating the stream as permanent. Plan for it to end. Resilience is covered in its own chapter.
Exercise
Why is Subscribe a bidirectional streaming call instead of a server streaming call, and what happens if your request generator stops yielding and returns?
Show answer
The client must send more than one message on the stream. Each FetchRequest tells the server how many additional events the client can accept, which is how flow control works. If the request generator returns, the client side of the stream closes, so you can no longer send FetchRequest messages and the subscription ends.