StreamObserver is the callback interface (io.grpc.stub.StreamObserver<V>) that gRPC Java uses to send and receive streams of messages. It has three methods: onNext(V) delivers one message, onError(Throwable) ends the stream with a failure, and onCompleted() ends it successfully. The same interface is used on both sides of a call, so one abstraction covers server streaming, client streaming and bidirectional streaming.
Even unary RPCs use it in the asynchronous stub and in the server implementation, where you call onNext once and then onCompleted.
A StreamObserver<V> represents one direction of a message flow. You either implement it to receive messages, or you are handed one to write messages to.
| Method | Meaning | Rules |
|---|---|---|
onNext(V value) | Receive or send one message | Call zero or more times |
onError(Throwable t) | Terminate the stream with an error | Terminal, nothing may follow |
onCompleted() | Terminate the stream normally | Terminal, nothing may follow |
Two rules matter in practice. First, exactly one terminal call (onError or onCompleted) must happen per stream, and no onNext may follow it. Second, a StreamObserver is not thread-safe: calls to the same instance must be serialised, so you cannot write to one observer from several threads at once without your own synchronisation.
The examples below use this Protocol Buffers definition with one RPC for each streaming style.
The generated base class is StreamingDemoGrpc.StreamingDemoImplBase, and the client gets a blocking stub, an async stub and a future stub.
In server streaming the server method receives the request and a StreamObserver<Response> to write to. Each onNext sends one message, and onCompleted closes the stream.
On the client side, the blocking stub exposes the stream as an Iterator<Response>, which is the simplest way to consume it.
The async stub instead takes a StreamObserver<Response> that you implement, as shown in the next section.
For client streaming the roles reverse. The server method returns a StreamObserver<Request> that gRPC calls for each incoming message, and it uses the responseObserver argument to send the single reply once the client finishes.
The client uses the async stub, because the call cannot return before the stream finishes. It passes an observer for the single response and gets back an observer to send requests into.
The latch is needed because the async stub returns immediately and the response arrives on a gRPC thread.
In a bidirectional call both sides hold a request-side and a response-side observer, and the two streams are independent. The server can reply to each message as it arrives, batch replies, or send messages with no request at all.
Because onNext for incoming messages is called by gRPC one at a time, writing back to responseObserver from inside it is safe. Writing from another thread, for example a timer, needs explicit synchronisation on the observer.
Report failures with a gRPC Status, not a raw exception, so the client receives a meaningful status code instead of UNKNOWN.
For long-lived streams, cast the server's responseObserver to ServerCallStreamObserver<Response>. It adds isCancelled(), setOnCancelHandler(Runnable) to stop work when the client goes away, and isReady() plus setOnReadyHandler(Runnable) for backpressure. Without these, a fast producer keeps buffering messages in memory while a slow client falls behind. For ClientCallStreamObserver on the client side, disableAutoRequest and request(n) let you control how many incoming messages you pull.
Set a deadline on every call (for example stub.withDeadlineAfter(30, TimeUnit.SECONDS)), otherwise a stuck stream can stay open indefinitely.
No. Calls to one StreamObserver instance must not run concurrently. If several threads produce messages, funnel them through one thread or synchronise on the observer.
The asynchronous stub and the server-side implementation share the same callback model. A unary handler calls onNext once and then onCompleted.
No. onCompleted and onError are terminal, and sending after them throws an IllegalStateException or is ignored depending on the implementation. Guard against it when several code paths can finish the stream.
You can for server streaming, where it returns an Iterator. Client and bidirectional streaming need the async stub, because requests and responses must flow at the same time.
StreamObserver is gRPC Java's single abstraction for message streams: onNext for each message, then exactly one of onError or onCompleted. Server streaming writes to the observer you are given, client streaming returns an observer to receive requests, and bidirectional streaming does both. Use Status for errors, set deadlines, never share one observer between threads without synchronisation, and use ServerCallStreamObserver for cancellation and backpressure on long-lived streams.