Is there a way to construct a grpc server on a connected socket net.conn?

612 views Asked by At

The typical usage in go is:

    server := grpc.NewServer()
    
    pb.RegisterUserServer(server, &userSrvc.UserServer{})

    addr := ":" + env.GetMyRpcPort()

    lis, _ := net.Listen("tcp", addr)

    server.Serve(lis)

I wonder can I run a server on a connected conn like:

conn, _ := net.Dial("tcp",":8080")
server.Serve(conn)

The client connect to the server first, and then construct a grpc server on the connected net.conn, then the server can rpc call the client to push some message as a request to the client.

1

There are 1 answers

0
Andrey Dyatlov On BEST ANSWER

I think that you need a bidirectional streaming. Here is one elaborated example of using bidirectional GRPC in Go: https://github.com/pahanini/go-grpc-bidirectional-streaming-example.

conn, err := grpc.Dial(":50005", grpc.WithInsecure())
...
client := pb.NewMathClient(conn)
stream, err := client.Max(context.Background())
...
req := pb.Request{Num: rnd}
err := stream.Send(&req)