@grpc/grpc-js vs @improbable-eng/grpc-web vs grpc-web
gRPC and gRPC-Web Libraries Comparison
1 Year
@grpc/grpc-js@improbable-eng/grpc-webgrpc-web
What's gRPC and gRPC-Web Libraries?

gRPC is a high-performance, open-source RPC (Remote Procedure Call) framework that uses HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features such as authentication, load balancing, and more. gRPC-Web is a technology that allows web applications to communicate with gRPC services directly from the browser, enabling the use of gRPC in frontend applications. This is particularly useful for modern web applications that require efficient, bi-directional communication with backend services.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@grpc/grpc-js13,596,8644,5791.94 MB20914 days agoApache-2.0
@improbable-eng/grpc-web174,1354,404-1803 years agoApache-2.0
grpc-web105,8888,77745.2 kB206a year agoApache-2.0
Feature Comparison: @grpc/grpc-js vs @improbable-eng/grpc-web vs grpc-web

gRPC Support

  • @grpc/grpc-js:

    @grpc/grpc-js provides full support for gRPC, including unary and streaming calls, authentication, and advanced features like metadata and error handling. It is a complete implementation of the gRPC protocol in JavaScript, making it suitable for all types of gRPC applications.

  • @improbable-eng/grpc-web:

    @improbable-eng/grpc-web offers support for gRPC-Web, including bi-directional streaming, which is not commonly supported in other gRPC-Web implementations. This makes it a unique choice for applications that require more advanced streaming capabilities while still being compatible with standard gRPC services.

  • grpc-web:

    grpc-web provides basic support for gRPC-Web, including unary and streaming calls. However, it does not support bi-directional streaming out of the box, making it more suitable for simpler use cases where advanced streaming features are not required.

Streaming Support

  • @grpc/grpc-js:

    @grpc/grpc-js supports both client-side and server-side streaming, allowing for more flexible and efficient data transfer between clients and servers. This is a key feature of gRPC that enables real-time communication and reduces latency in data transmission.

  • @improbable-eng/grpc-web:

    @improbable-eng/grpc-web supports bi-directional streaming, allowing both the client and server to send and receive messages simultaneously. This feature is particularly useful for applications that require real-time data exchange and can significantly improve performance and responsiveness.

  • grpc-web:

    grpc-web supports server-side streaming and client-side streaming, but does not support bi-directional streaming. This makes it suitable for applications that need one-way streaming but do not require the complexity of bi-directional communication.

Ease of Integration

  • @grpc/grpc-js:

    @grpc/grpc-js integrates seamlessly with existing gRPC services and supports all standard gRPC features, making it easy to use in a wide range of applications. Its compatibility with the gRPC ecosystem ensures that it works well with other gRPC tools and libraries.

  • @improbable-eng/grpc-web:

    @improbable-eng/grpc-web is designed to work with existing gRPC services and requires minimal changes to the server-side implementation. Its lightweight design and support for standard gRPC features make it easy to integrate into existing projects.

  • grpc-web:

    grpc-web provides a simple and straightforward API for integrating gRPC into web applications. It is easy to set up and use, making it a good choice for projects that need quick and simple gRPC functionality without a lot of overhead.

Code Example

  • @grpc/grpc-js:

    gRPC Client Example with @grpc/grpc-js

    const grpc = require('@grpc/grpc-js');
    const protoLoader = require('@grpc/proto-loader');
    
    // Load the protobuf
    const packageDefinition = protoLoader.loadSync('path/to/protofile.proto');
    const proto = grpc.loadPackageDefinition(packageDefinition);
    
    // Create a client
    const client = new proto.ServiceName('localhost:50051', grpc.credentials.createInsecure());
    
    // Make a unary call
    client.UnaryMethodName({ request: 'data' }, (error, response) => {
      if (error) {
        console.error('Error:', error);
      } else {
        console.log('Response:', response);
      }
    });
    
    // Make a streaming call
    const call = client.StreamingMethodName();
    call.on('data', (data) => {
      console.log('Streaming data:', data);
    });
    call.on('end', () => {
      console.log('Stream ended');
    });
    call.on('error', (error) => {
      console.error('Stream error:', error);
    });
    
    // Make a bi-directional streaming call
    const biCall = client.BiDirectionalStreamingMethod();
    biCall.on('data', (data) => {
      console.log('Received:', data);
      biCall.write({ response: 'data' }); // Send data back
    });
    biCall.on('end', () => {
      console.log('Bi-directional stream ended');
    });
    biCall.on('error', (error) => {
      console.error('Bi-directional stream error:', error);
    });
    
  • @improbable-eng/grpc-web:

    gRPC-Web Client Example with @improbable-eng/grpc-web

    import { grpc } from '@improbable-eng/grpc-web';
    import { ServiceName } from './path/to/protofile_pb';
    
    // Unary call
    grpc.unary(ServiceName.UnaryMethodName, {
      request: { data: 'data' },
      host: 'http://localhost:50051',
      onEnd: (response) => {
        const { status, message } = response;
        if (status === 200) {
          console.log('Response:', message);
        } else {
          console.error('Error:', status, message);
        }
      },
    });
    
    // Bi-directional streaming
    const stream = grpc.invoke(ServiceName.BiDirectionalStreamingMethod, {
      request: { data: 'data' },
      host: 'http://localhost:50051',
      onMessage: (message) => {
        console.log('Received:', message);
      },
      onEnd: (status, message) => {
        console.log('Stream ended:', status, message);
      },
    });
    
    // Send a message
    stream.send({ data: 'data' });
    
  • grpc-web:

    gRPC-Web Client Example with grpc-web

    import { grpc } from '@improbable-eng/grpc-web';
    import { ServiceName } from './path/to/protofile_pb';
    
    // Unary call
    grpc.unary(ServiceName.UnaryMethodName, {
      request: { data: 'data' },
      host: 'http://localhost:50051',
      onEnd: (response) => {
        const { status, message } = response;
        if (status === 200) {
          console.log('Response:', message);
        } else {
          console.error('Error:', status, message);
        }
      },
    });
    
    // Streaming call
    const stream = grpc.invoke(ServiceName.StreamingMethodName, {
      request: { data: 'data' },
      host: 'http://localhost:50051',
      onMessage: (message) => {
        console.log('Received:', message);
      },
      onEnd: (status, message) => {
        console.log('Stream ended:', status, message);
      },
    });
    
How to Choose: @grpc/grpc-js vs @improbable-eng/grpc-web vs grpc-web
  • @grpc/grpc-js:

    Choose @grpc/grpc-js if you are building a Node.js application that requires full gRPC capabilities, including support for streaming, authentication, and advanced features. This package is pure JavaScript and does not require native modules, making it easier to use across different environments.

  • @improbable-eng/grpc-web:

    Choose @improbable-eng/grpc-web if you need a gRPC-Web client that supports bi-directional streaming and works well with existing gRPC services. This package is designed to be lightweight and efficient, making it a good choice for applications that require low-latency communication.

  • grpc-web:

    Choose grpc-web if you are looking for a simple and straightforward gRPC-Web client for your web applications. This package provides basic gRPC-Web functionality and is easy to integrate with existing gRPC services, making it suitable for projects that need quick and simple gRPC support.

README for @grpc/grpc-js

Pure JavaScript gRPC Client

Installation

Node 12 is recommended. The exact set of compatible Node versions can be found in the engines field of the package.json file.

npm install @grpc/grpc-js

Documentation

Documentation specifically for the @grpc/grpc-js package is currently not available. However, documentation is available for the grpc package, and the two packages contain mostly the same interface. There are a few notable differences, however, and these differences are noted in the "Migrating from grpc" section below.

Features

  • Clients
  • Automatic reconnection
  • Servers
  • Streaming
  • Metadata
  • Partial compression support: clients can compress and decompress messages, and servers can decompress request messages
  • Pick first and round robin load balancing policies
  • Client Interceptors
  • Connection Keepalives
  • HTTP Connect support (proxies)

If you need a feature from the grpc package that is not provided by the @grpc/grpc-js, please file a feature request with that information.

This library does not directly handle .proto files. To use .proto files with this library we recommend using the @grpc/proto-loader package.

Migrating from grpc

@grpc/grpc-js is almost a drop-in replacement for grpc, but you may need to make a few code changes to use it:

  • If you are currently loading .proto files using grpc.load, that function is not available in this library. You should instead load your .proto files using @grpc/proto-loader and load the resulting package definition objects into @grpc/grpc-js using grpc.loadPackageDefinition.
  • If you are currently loading packages generated by grpc-tools, you should instead generate your files using the generate_package_definition option in grpc-tools, then load the object exported by the generated file into @grpc/grpc-js using grpc.loadPackageDefinition.
  • If you have a server and you are using Server#bind to bind ports, you will need to use Server#bindAsync instead.
  • If you are using any channel options supported in grpc but not supported in @grpc/grpc-js, you may need to adjust your code to handle the different behavior. Refer to the list of supported options below.
  • Refer to the detailed package comparison for more details on the differences between grpc and @grpc/grpc-js.

Supported Channel Options

Many channel arguments supported in grpc are not supported in @grpc/grpc-js. The channel arguments supported by @grpc/grpc-js are:

  • grpc.ssl_target_name_override
  • grpc.primary_user_agent
  • grpc.secondary_user_agent
  • grpc.default_authority
  • grpc.keepalive_time_ms
  • grpc.keepalive_timeout_ms
  • grpc.keepalive_permit_without_calls
  • grpc.service_config
  • grpc.max_concurrent_streams
  • grpc.initial_reconnect_backoff_ms
  • grpc.max_reconnect_backoff_ms
  • grpc.use_local_subchannel_pool
  • grpc.max_send_message_length
  • grpc.max_receive_message_length
  • grpc.enable_http_proxy
  • grpc.default_compression_algorithm
  • grpc.enable_channelz
  • grpc.dns_min_time_between_resolutions_ms
  • grpc.enable_retries
  • grpc.max_connection_age_ms
  • grpc.max_connection_age_grace_ms
  • grpc.max_connection_idle_ms
  • grpc.per_rpc_retry_buffer_size
  • grpc.retry_buffer_size
  • grpc.service_config_disable_resolution
  • grpc.client_idle_timeout_ms
  • grpc-node.max_session_memory
  • grpc-node.tls_enable_trace
  • grpc-node.retry_max_attempts_limit
  • channelOverride
  • channelFactoryOverride

Some Notes on API Guarantees

The public API of this library follows semantic versioning, with some caveats:

  • Some methods are prefixed with an underscore. These methods are internal and should not be considered part of the public API.
  • The class Call is only exposed due to limitations of TypeScript. It should not be considered part of the public API.
  • In general, any API that is exposed by this library but is not exposed by the grpc library is likely an error and should not be considered part of the public API.
  • The grpc.experimental namespace contains APIs that have not stabilized. Any API in that namespace may break in any minor version update.