Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to use gRPC to push data?

Tags:

go

grpc

I'm wondering if it's a good idea to push data from gRPC server to a client. Basically I want to use a pub/sub pattern with gRPC. The way I do is that I return a response stream on the server implementation that I never close. Then, the client has a never ending go routine in charge of reading this stream.

Here is an example:

service Service {
    rpc RegularChanges (Void) returns (stream Change) {}
}

On the server side:

func (self *MyServiceImpl) RegularChanges(in *pb.Void, stream pb.Service_RegularChangesServer) error {

    for {
        d, err := time.ParseDuration("1s")
        if err != nil {
            log.Fatalf("Cannot parse duration")
            break;
        }
        time.Sleep(d)
        stream.Send(&pb.Change{Name:"toto", Description:"status changed"})
    }
    return nil
}

On client:

for {
        change, err := streamChanges.Recv()
        if err != nil {
            log.Fatalf("Error retrieving change")
        } else {
            log.Println(change)
        }
}

I just began with go and gRPC but I know it's based on HTTP2, hence it should support pushing datas. However, I'm not sure this is the way gRPC should be used.

like image 662
Momh Avatar asked Apr 25 '17 21:04

Momh


People also ask

Does gRPC support push?

gRPC does not currently support/use PUSH_PROMISE. Streaming RPCs in gRPC use HTTP/2 streams; the entire RPC is contained in a request/response in HTTP.

Can gRPC replace REST?

gRPC uses the Protobuf (protocol buffers) messaging format, which is a highly-packed, highly-efficient messaging format for serializing structured data. For a specific set of use-cases, a gRPC API can serve as a more efficient alternative to a REST API (more on this later).

Is gRPC faster than WebSocket?

gRPC is preferred over WebSockets if the application demands various requests processing at one time. gRPC supports multiplexing that arranges different requests seamlessly. But, multiplexing isn't offered by WebSocket. Hence, one connection is useful only for one request.


1 Answers

gRPC is intended to be used in this way.

You should still consider how the client should behave on failures and how you may want to re-balance across backends. If your connection is going across the Internet, you may also want to enable keepalive to detect connection breakages by providing KeepaliveParams to the client and server.

like image 160
Eric Anderson Avatar answered Nov 04 '22 21:11

Eric Anderson