- for studying grpc
- implement server side by golang
- implement client side by golang and flutter
- GetFeature Simple RPC
- ListFeatures Server-side streaming RPC
- RecordRoute Client-side streaming RPC
- RouteChat Bidirectional streaming RPC
- create
.proto
file likesimple_grpc.proto
- run
protoc --go_out=. --go_opt=paths=source_relative \ --go-grpc_out=. --go-grpc_opt=paths=source_relative \ simplegrpc/simple_grpc.proto
- server implements
SimpleGrpcServer interface
and client implementsSimpleGrpcClient interface
. These files are insimplegrpc/simple_grpc_grpc.pb.go
- start the server
grpcServer := grpc.NewServer(opts...) pb.RegisterSimpleGrpcServer(grpcServer, newServer()) grpcServer.Serve(lis)
- create the channel on client
var opts []grpc.DialOption conn, err := grpc.Dial(*serverAddr, opts...) if err != nil { ... } defer conn.Close() client := pb.NewRouteGuideClient(conn)
- client can call our service methods via
client
val
- run the server
go run server/server.go
- run the client
go run client/client.go
you will see following outputs
Getting feature for point (409146138, -746188906)
name:"Berkshire Valley Management Area Trail, Jefferson, NJ, USA" location:<latitude:409146138 longitude:-746188906 >
Getting feature for point (0, 0)
location:<>
Looking for features within lo:<latitude:400000000 longitude:-750000000 > hi:<latitude:420000000 longitude:-730000000 >
name:"Patriots Path, Mendham, NJ 07945, USA" location:<latitude:407838351 longitude:-746143763 >
...
name:"3 Hasta Way, Newton, NJ 07860, USA" location:<latitude:410248224 longitude:-747127767 >
Traversing 56 points.
Route summary: point_count:56 distance:497013163
Got message First message at point(0, 1)
Got message Second message at point(0, 2)
Got message Third message at point(0, 3)
Got message First message at point(0, 1)
Got message Fourth message at point(0, 1)
Got message Second message at point(0, 2)
Got message Fifth message at point(0, 2)
Got message Third message at point(0, 3)
Got message Sixth message at point(0, 3)
- reference
- install grpc package
- install protoc-gen-dart
dart pub global activate protoc_plugin
- generate client stub
protoc --dart_out=grpc:client/flutter_client/lib/generated -Isimplegrpc simplegrpc/simple_grpc.proto
- run golang server
go run server/server.go
- run flutter app
- tap button