Why grpc-go can run grpc server and http server at the same address and port, but grpc-node cannot
Asked Answered
C

2

8

I had read this answer: https://mcmap.net/q/1326468/-can-grpc-and-express-server-run-by-same-nodejs-server-or-grpc-has-to-be-different-server, it says that there is no way to run gRPC server and HTTP server at same address and port using grpc-node package.

But I can create gRPC server and HTTP server at same address and port(e.g. both using localhost:3000) using grpc-go package. Here is an example: https://github.com/mrdulin/grpc-go-cnode/blob/master/cmd/server/main.go#L79

So, why grpc-node and grpc-go behave inconsistently. Does this make sense?

The result I expect is that no matter what language is implemented in grpc, the behavior should be consistent. So the grpc server should be able to share the same port with the server created by Node's standard library http in same system process.

Concentrated answered 31/8, 2020 at 9:45 Comment(0)
A
10

It is all about implementation. Each language has its own implementation for gRPC. There are many differences from each language implementation, some due to language capability but also due to the maintainers. Each project is a different project.

In this case, we can not really say that gRPC and HTTP servers are sharing the same address. There is only the HTTP server running. However, Golang implementation for gRPC server has an option to serv the gRPC through HTTP.

Calling

server.ServeHTTP()

instead of

server.Serve()

That is possible because, under the hood, gRPC server is built on top HTTP2

This snippet from the link you shared make what I said clear

if request.ProtoMajor != 2 {
            mux.ServeHTTP(writer, request)
            return
}

if strings.Contains(request.Header.Get("Content-Type"), "application/grpc") {
                grpcServer.ServeHTTP(writer, request)
                return
}

If you want to do the same in Node, you need to check in the grpc-node implementation if there is such a thing available

Amabil answered 1/9, 2020 at 22:5 Comment(0)
C
2

Your example uses http.NewServeMux(), which is provided by the Go standard library. The Node standard library does not provide an equivalent feature, so you can't share the port that way.

Costanza answered 31/8, 2020 at 16:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.