
gRPC on Android: Proto, Channel & Streaming Calls
gRPC on Android: Proto, Channel & Streaming Calls gRPC provides type-safe, efficient communication using protocol buffers. On Android, gRPC reduces bandwidth and latency for mobile apps. Defining Proto Services syntax = "proto3" ; package myapp ; service UserService { rpc GetUser ( UserId ) returns ( User ); rpc ListUsers ( Empty ) returns ( stream User ); rpc UpdateUser ( User ) returns ( Empty ); } message UserId { int32 id = 1 ; } message User { int32 id = 1 ; string name = 2 ; string email = 3 ; } Creating gRPC Channel class GrpcClient ( private val host : String , private val port : Int ) { private val channel = ManagedChannelBuilder . forAddress ( host , port ) . usePlaintext () . build () private val stub = UserServiceGrpc . newBlockingStub ( channel ) fun getUser ( id : Int ): User { return stub . getUser ( UserId . newBuilder (). setId ( id ). build ()) } fun shutdown () { channel . shutdown (). awaitTermination ( 5 , TimeUnit . SECONDS ) } } Server Streaming val users = mutab
Continue reading on Dev.to Tutorial
Opens in a new tab

