gRPC on Node.js

repository·master·Indexed 26 days ago

https://github.com/grpc/grpc-node

A suite of tools and libraries for implementing gRPC clients and servers in Node.js. Includes @grpc/grpc-js for pure JavaScript implementation, @grpc/proto-loader for loading .proto files, @grpc/reflection for the Server Reflection Protocol, grpc-health-check for health monitoring, and grpc-tools for generating code via protoc. Also provides @grpc/grpc-js-xds for xDS support.

Tokens
20.6K
Snippets
36
Records
139
Agent score
89%

What's inside grpc-node

  1. Use the pure JavaScript gRPC implementation

    master
    For modern Node.js applications, use the @grpc/grpc-js package. This is a pure JavaScript implementation of the core gRPC functionality that does not require a C++ addon. It is compatible with the latest versions of Node.js across all supported platforms.
  2. Implement the gRPC Health Checking Protocol on a Server

    master

    To allow a gRPC-node server to support health checks, use the HealthImplementation class.

    1. Define a statusMap where keys are service names and values are statuses (e.g., 'SERVING', 'NOT_SERVING').
    2. Use an empty string '' as a key to represent the status of the entire server.
    3. Instantiate HealthImplementation with your map.
    4. Call healthImpl.addToServer(server) to attach the service to your existing server.
    5. Use healthImpl.setStatus(serviceName, status) to dynamically update the status of specific services.
    // Import package
    import { HealthImplementation, ServingStatusMap } from 'grpc-health-check';
    
    // Define service status map. Key is the service name, value is the corresponding status.
    // By convention, the empty string '' key represents that status of the entire server.
    const statusMap = {
      'ServiceFoo': 'SERVING',
      'ServiceBar': 'NOT_SERVING',
      '': 'NOT_SERVING',
    };
    
    // Construct the service implementation
    const healthImpl = new HealthImplementation(statusMap);
    
    healthImpl.addToServer(server);
    
    // When ServiceBar comes up
    healthImpl.setStatus('serviceBar', 'SERVING');
  3. Configure client-side message compression

    master

    To configure message compression on a gRPC client, pass a configuration object containing compression settings as the third argument when instantiating the client.

    Use the following options to control behavior:

    • grpc.default_compression_algorithm (int): Sets the default compression algorithm for the channel (applies to sending messages).

      • 0: No compression
      • 1: DEFLATE algorithm
      • 2: GZIP algorithm
      • 3: Stream compression with GZIP algorithm
    • grpc.default_compression_level (int): Sets the default compression level for the channel (applies to receiving messages).

      • 0: None
      • 1: Low level
      • 2: Medium level
      • 3: High level
    client = new ExampleClient("example.com", credentials.createInsecure(), {'grpc.default_compression_algorithm': 2, 'grpc.default_compression_level': 2});
  4. Load Protobuf files with @grpc/proto-loader

    master
    Use the @grpc/proto-loader package to load .proto files into objects. These objects can then be passed directly to gRPC client or server implementations to define your service interfaces.
  5. Implement gRPC Reflection API Service

    master
    The @grpc/reflection package provides a Reflection API service for gRPC servers. This allows clients to query the server for information about the services and methods it provides.
  6. Use grpc-tools to generate gRPC code

    master
    The grpc-tools package provides the grpc_tools_node_protoc executable, which is a wrapper around the Protocol Buffers compiler protoc. It includes the Node gRPC plugin by default, allowing you to generate client and service objects for Node.js. It accepts the same arguments as protoc and specifically supports the --grpc_out flag.
  7. Generate TypeScript types for Protobuf definitions

    master

    Use the proto-loader-gen-types CLI tool to generate TypeScript type information for your .proto files. This allows you to have type safety when working with the objects loaded at runtime.

    Example Command:

    $(npm bin)/proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto