Hello
As we know, Server-side notification/push can be achieved by multiple ways depending upon what you want to achieve.
In my use-case , the built-in HTTP/2 SERVER_PUSH frames based approach is NOT feasible.
Another approach is web-socket. But that does not fit very well with HTTP/2
Web-push API is also not a good fit because it is based on service-worker and more heavy-weight compared to some in-line technology.
In this context, I believe I can do it with HTTP/2 + server-side-event and leverage Event-Source Mechanism.
However , I am interested in exploring if some streaming-based gRPC API can help me.
Basically, in my use-case I want the server to send the data if/when available with him.
So in simple terms
I invoke one gRPC server-side streaming API in asynchronous, non-blocking mode and then continue on my client-side browser work.
The implementation of gRPC server-side code will just push the data whenever available by calling streamObserver.onNext(response);
If data is not available the server-side method will just sleep for a while before checking for the next time.
Basically, the underlying design here is the HTTP/2 stream that is spawned will remain open forever on that TCP connection.
Do you think this is scalable enterprise level architecture ? Can we keep the HTTP/2 stream open for long time ?
Thanks.
public class StockServiceImpl extends StockServiceGrpc.StockServiceImplBase
{
public void stockUpdate(StockServiceOuterClass.HelloRequest request, StreamObserver<StockServiceOuterClass.HelloResponse> streamObserver)
{
//run a loop
{
//check if response object can be constructed by checking our DB
//if it can be , then create it and hand it over to the streamObserver
StockServiceOuterClass.HelloResponse response = StockServiceOuterClass.HelloResponse.newBuilder().setStockInfo(“new data from DB”).build();
streamObserver.onNext(response);
//but if NO new data available in DB, just sleep for a while like
Thread.sleep(30 sec);
//check again if any data is available in our DB to create a response object
}
// When you are fully done, you amy call onCompleted.
//technically this line will close the underlying http/2 stream which I would not do it very quickly
streamObserver.onCompleted();
}
}