Streaming RPC and versioning
Prerequisite: node-grpc-deadlines-and-errors. Objective: Review backward-compatible schema change. Each example is independent. Tested with Node.js 22.16.0 on Linux. Save as main.mjs in a fresh disposable directory and run node main.mjs. These are static code blocks; external system behavior is not established by a local model.
How it works
Messages retain their order within each individual gRPC stream. Stream lifetime, limits, cancellation and backpressure need explicit application design. Additive fields can be binary-compatible but may still break application assumptions, JSON consumers or strict validators. Reserve deleted field numbers and names; never reuse an old number for unrelated meaning. Test old and new clients in both directions.
Consume a bounded model stream
import assert from 'node:assert/strict';let closed=false;async function* books(){try{for(const id of ['a','b','c'])yield id;}finally{closed=true;}}const found=[];for await(const id of books()){found.push(id);if(found.length===2)break;}assert.equal(closed,true);console.log(found.join(','));Expected output:
a,bEarly exit closes the generator; transport flow control is separate.
Reject reuse of a reserved tag
import assert from 'node:assert/strict';const reserved=new Set([2]);function allowed(fields){return fields.every(f=>!reserved.has(f.number))&&new Set(fields.map(f=>f.number)).size===fields.length;}assert.equal(allowed([{number:1},{number:2}]),false);console.log(allowed([{number:1},{number:3}]));Expected output:
trueThe model only checks reservations and duplicates, not complete schema compatibility.
Common mistake
Do not equate an async generator with an actual gRPC transport or assume compatible bytes imply compatible business behavior.
Practice
Design a catalog stream capped at two messages and propose a safe optional description field.
Reference candidate: node-streaming-rpc-and-versioning-lookup (route unverified). Documentation anchors: https://grpc.io/docs/what-is-grpc/core-concepts/, https://protobuf.dev/programming-guides/proto3/. External versions and full integration are pending.
Administrative staged lab: staged-deployment-labs/rpc/catalog.proto. Read its README for prerequisites, execution boundaries and cleanup. Not a verified public route.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Streaming RPC and versioning — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Which call accepts one request and multiple replies?
What should happen to a removed field number?
What does the generator cleanup test establish?