Hello,
I have a server with several clients. From time to time, the server must send a notification to some clients.
I can’t find a proper way to do this. I had 2 different solutions to do that:
1. Create an RPC function which returns a stream of notifications.
Something like:
rpc GetNotification(google.protobuf.Empty) returns (stream Notification) {}
The client would call this function “permanently” in a dedicated thread with the async interface:
stub->AsyncGetNotification(...)
...
while (!stopped) {
nextStatus = completionQueue.AsyncNext(...);
if (nextStatus == grpc::CompletionQueue::GOT_EVENT) {
// handle the notification
...
}
}
On the server side, the GetNotification function would just write the notification when there is no notification to send, and would do nothing the rest of the time.
But this means that there would be as many GetNotification functions executed at the same time as there are connected clients. I don’t expect to have much clients running at the same time (let’s say ~10-20), but I don’t like that much.
2. Create a simple RPC function which returns a notification:
rpc GetNotification(google.protobuf.Empty) returns (Notification) {}
The client would call it periodically (let’s say every 5 seconds) and would handle the notification if there is one, and would do nothing if there is none.
On the server side, the GetNotification function would return an empty notification if there is none, or the notification with the appropriate data when needed.
I don’t like this solution much because it is not really efficient.
Is there a better solution than the two above ones?
Otherwise, which one is the best?
Thanks.
Julien