2
votes

I am the first scholar of grpc. I think it is an ugly question.

A simple chat application is created using grpc-go. and want to achieve is something like this.

@Each of clientA, clientB, clientC, connects to serverA, and has a bidirectional stream connection. For example, if you want to notify clientA, B, C that clientD has connected to serverA, what kind of implementation method is there?

How to broadcast in gRPC from server to client?

gRPC: How can I distinguish bi-streaming clients at server side?

I have read these posts, but I would like to know the best practices.

For example, when creating a client list and notifying there, how should this be code?

1

1 Answers

0
votes

I wrote it down because it was self-solving.

https://github.com/rodaine/grpc-chat

This project was very helpful.

type server struct {
    ClientStreams map [string] chan chat.StreamResponse
}

In this way, a map that stores the stream of the client, here map, but slice or sync.Map may be used.

By connecting the information from the client to the structure when the stream was opened, we were able to support broadcasting and managing the number of connections.