blog

12019-06-10

SEAN K.H. LIAO

grpc-web

grpc-web has been out for some time, can we finally use it?

what works

what doesn't

crash course client side

write your service.proto

protoc service.proto --js_out=import_style=commonjs:. --grpc-web_out=import_style=commonjs,mode=grpcwebtext:. where :. specifies the output directory, service_pb.js will contain the message types, and service_grpc_web_pb will contain the client types

initialize with let svc = new ServiceClient();, and use with let call = svc.rpcName(msg, {metadata: options}, (err, res)=>{/* do something */})

crash course server side

using improbable-eng/grpc-web

with go, implement grpc server, your server: svr := NewServer(), grpc server: gsvr := grpc.NewServer() register: service.RegisterServiceServer(gsvr, svr)

wrap in grpcweb handler, wsvr := grpcweb.WrapServer(gsvr)

serve: http.ListenAndServe("80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){wsvr.ServeHTTP(w, r)}))

notes: authentication / authorization should be handled as a grpc.UnaryInterceptor provided to grpc.NewServer(), cross origin should be handled by grpcweb.WithOriginFunc(), other headers whould be handled by grpcweb.WithAllowRequestHeader([]string{"*"})