1 minute read

Categories:
Tags:

Realtime pushed-based databases such as Google Firebase are a convenient way to ensure clients have the most recent data locally. Data updates are automatically streamed to clients immediately as they happen, or in the case of a client disconnect, immediately after reconnecting.

gRPC server streaming and ZIO Hub allow this functionality to be easily replicated and customized beyond what expensive paid-for services such as Firebase can do.

Client demands for data streaming

The typical simple web-based client-server communication pattern is for data to be requested by the client. When clients want new data or otherwise interact, it will initiate a new request to the server. But as server technology and hardware capacities have increased, user expectations have increased to expect all client UIs to present realtime data without the friction of manually requesting data updates. The typical client-server communication is slowly evolving into the stream of data in both directions between the client and server.

gRPC Server Bi-Directional Streaming using HTTP/2

The evolution of technology has resulted in 2 technology standards for web-based bi-directional communications:
WebSockets and HTTP/2 streams.

WebSockets were created first as the ability for a standard HTTP/1.1 connection to upgrade to support bi-directional client-server streaming. This is still the best approach for browser-server communications because of its clear JavaScript APIs within all browsers, backwards compatibility for HTTP/1.1-only clients, and its ability to take advantage of performance improvements offered by HTTP/2 and beyond.

For non-browser communications, such as with mobile apps or inter-server communication WebSockets is an unnecessary layer. As WebSockets runs over HTTP, because HTTP/2 has directly integrated multiplexed streaming capabilities it is better for abstraction libraries such as gRPC to directly support HTTP/2 instead of the higher-level WebSocket layer.

service SyncService {
  rpc Bidirectional (stream Request) returns (stream Response);
}
def bidirectional(request: Stream[StatusException, Request]): Stream[StatusException, Response] =
  //request.flatMap:
  //  Request => Stream[StatusException, Response]

ZIO Hub for Concurrency and Subscriptions

message SyncRequest {
  repeated Subscribe subscribes = 1;
  repeated Unsubscribe unsubscribes = 2;
}
message SyncResponse {
  Data data = 1;
}

//TODO:

Realtime database pushing updates to clients using bi-directional gRPC Streams
Realtime database pushing updates to clients using bi-directional gRPC Streams

Sources

Categories:
Tags:
Updated: