Question:
Can one utilize gRPC to push data from server to client? Consider a pub/sub pattern with an infinite response stream on server and a never-ending routine on client that constantly reads from this stream.
// Service proto service Service { rpc RegularChanges (Void) returns (stream Change) {} } // Server implementation func (self *MyServiceImpl) RegularChanges(in *pb.Void, stream pb.Service_RegularChangesServer) error { for { stream.Send(&pb.Change{Name:"toto", Description:"status changed"}) time.Sleep(d) } return nil } // Client for { change, err := streamChanges.Recv() if err != nil { // Handle error } else { // Process change } }
Answer:
gRPC is designed for such usage, facilitating a publish/subscribe pattern between client and server. However, certain considerations should be addressed:
The above is the detailed content of Can gRPC Stream Data from Server to Client in a Pub/Sub Pattern?. For more information, please follow other related articles on the PHP Chinese website!