DEV Community

23n6
23n6

Posted on

gRpc streaming client typescript

A client-gRPC streaming written in TypeScript for browser applications. The focus is on simplicity, reliability, and extensibility. It provides convenient features to automatically reconnect and convert data to object.

Features

  • Dependency-free and small in size
  • When the connection is lost, it can optionally be configured to
    • Automatically try to reconnect in a smart way
  • Easy to override metadata and request
  • Support uses the backoff factor to apply between attempts.
  • Builder-class for easy initialization and configuration
  • High test-coverage and in-code documentation
    • Enables you to easily modify and extend the code

Installation

In your project-root:

🚀 Install with npm

npm install @nixjs23n6/grpc-stream-client
Enter fullscreen mode Exit fullscreen mode

🚀 Install with yarn

yarn add @nixjs23n6/grpc-stream-client
Enter fullscreen mode Exit fullscreen mode

In your project-code:

import { StreamBuilder } from '@nixjs23n6/grpc-stream-client';
Enter fullscreen mode Exit fullscreen mode

Usage

Example proto:

export class ChatClient {
  constructor (hostname: string,
               credentials?: null | { [index: string]: string; },
               options?: null | { [index: string]: any; });

  room(
    request: JoinRoomRequest,
    metadata?: grpcWeb.Metadata
  ): grpcWeb.ClientReadableStream<RoomEvent>;
}
Enter fullscreen mode Exit fullscreen mode

Initialization

Create a new instance with the provided StreamBuilder:

const client = new ChatClient()
const stream = new StreamBuilder(client, 'room').build();
Enter fullscreen mode Exit fullscreen mode

Request

Add a request into builder:

const client = new ChatClient()
const stream = new StreamBuilder(client, 'room').withRequest(new google_protobuf_empty_pb.Empty()).build();
Enter fullscreen mode Exit fullscreen mode

Metadata

Add a metada into buider:

const client = new ChatClient()
let deadline = new Date()
deadline.setSeconds(deadline.getSeconds() + BaseTimeoutStreamingRequest).toString()
const authorization = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
const stream = new StreamBuilder(client, 'room').withMetadata({ deadline, authorization }).build();
Enter fullscreen mode Exit fullscreen mode

Events & Callbacks

There are five events which can be subscribed to through callbacks:

export enum StreamEvents {
    data = 'data', // A data was received
    metadata = 'metadata', // A metadata of the connection
    error = 'error', // An error occurred
    end = 'end', // Connection is ended
    retry = 'retry', // A try to re-connect is made
}
Enter fullscreen mode Exit fullscreen mode

The callbacks are called with the issuing streaming-instance and the causing event as arguments:

const client = new ChatClient()
const stream = new StreamBuilder(client, 'room')
    .onData((i, data) => console.log('[Stream] data', data)))
    .onEnd((i) => console.log('[Stream] Ended'))
    .onError((i, error) => console.log('[Stream] Error', error))
    .onRetry((i, e) => console.log('[Stream] Retry', e))
    .build();
Enter fullscreen mode Exit fullscreen mode

You can remove event-listener with removeEventListener:

let stream: Stream
/* ... */
stream.removeEventListener(StreamEvents.data, dataEventListener);
Enter fullscreen mode Exit fullscreen mode

Retry advanced

You can update metadata or request when using onRetry method

const client = new ChatClient()
const stream = new StreamBuilder(client, 'room')
    .onRetry(async (instance) => {
        const token = await requestNewAccessToken()
            instance.metaData = {
               authorization: token
                deadline
            }
        })
    .build();
Enter fullscreen mode Exit fullscreen mode

Reconnect & Backoff

If you want the gRpc streaming to automatically try to re-connect when the connection is lost, you can provide it with a Backoff.
The streaming will use the Backoff to determine how long it should wait between re-tries. There are currently three
Backoff-implementations. You can also implement your own by inheriting from the Backoff-interface.

To use the Backoff feature, install package:

yarn add @nixjs23n6/backoff-typescript
// Or 
npm install @nixjs23n6/backoff-typescript
Enter fullscreen mode Exit fullscreen mode
ConstantBackoff

The ConstantBackoff will make the streaming wait a constant time between each connection retry. To use the ConstantBackoff
with a wait-time of 1 second:

const client = new ChatClient()
const stream = new StreamBuilder(client, 'room')
    .withBackoff(new ConstantBackoff(1000)) // 1000ms = 1s
    .build();
Enter fullscreen mode Exit fullscreen mode
LinearBackoff

The LinearBackoff linearly increases the wait-time between connection-retries until an optional maximum is reached.
To use the LinearBackoff to initially wait 0 seconds and increase the wait-time by 1 second with every retry until
a maximum of 8 seconds is reached:

const client = new ChatClient()
const stream = new StreamBuilder(client, 'room')
    .withBackoff(new LinearBackoff(0, 1000, 8000))
    .build();
Enter fullscreen mode Exit fullscreen mode
ExponentialBackoff

The ExponentialBackoff doubles the backoff with every retry until a maximum is reached. This is modelled after the binary
exponential-backoff algorithm used in computer-networking. To use the ExponentialBackoff that will produce the series
[100, 200, 400, 800, 1600, 3200, 6400]:

const client = new ChatClient()
const stream = new StreamBuilder(client, 'room')
    .withBackoff(new ExponentialBackoff(100, 7))
    .build();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay