@improbable-eng/grpc-web
TypeScript icon, indicating that this package has built-in type declarations

0.15.0 • Public • Published

@improbable-eng/grpc-web

Library for making gRPC-Web requests from a browser

This library is intended for both JavaScript and TypeScript usage from a web browser or NodeJS (see Usage with NodeJS).

Note: This only works if the server supports gRPC-Web

A Golang gRPC-Web middleware and a Golang-based gRPC-Web proxy are available here.

Please see the full gRPC-Web README for known limitations.

Installation

@improbable-eng/grpc-web has peer dependencies of google-protobuf and @types/google-protobuf.

npm install google-protobuf @types/google-protobuf @improbable-eng/grpc-web --save

Example Project

There is an example project available here

Usage Overview

import {grpc} from "@improbable-eng/grpc-web";

// Import code-generated data structures.
import {BookService} from "./generated/proto/examplecom/library/book_service_pb_service";
import {GetBookRequest} from "./generated/proto/examplecom/library/book_service_pb";

const getBookRequest = new GetBookRequest();
getBookRequest.setIsbn(60929871);
grpc.unary(BookService.GetBook, {
  request: getBookRequest,
  host: host,
  onEnd: res => {
    const { status, statusMessage, headers, message, trailers } = res;
    if (status === grpc.Code.OK && message) {
      console.log("all ok. got book: ", message.toObject());
    }
  }
});
  • Requests can be aborted/cancelled before they complete:
const request = grpc.unary(BookService.GetBook, { ... });
request.cancel();

Available Request Functions

There are three functions for making gRPC requests:

grpc.unary

This is a convenience function for making requests that consist of a single request message and single response message. It can only be used with unary methods.

rpc GetBook(GetBookRequest) returns (Book) {}

grpc.invoke

This is a convenience function for making requests that consist of a single request message and a stream of response messages (server-streaming). It can also be used with unary methods.

rpc GetBook(GetBookRequest) returns (Book) {}
rpc QueryBooks(QueryBooksRequest) returns (stream Book) {}

grpc.client

grpc.client returns a client. Dependant upon transport compatibility this client is capable of sending multiple request messages (client-streaming) and receiving multiple response messages (server-streaming). It can be used with any type of method, but will enforce limiting the sending of messages for unary methods.

rpc GetBook(GetBookRequest) returns (Book) {}
rpc QueryBooks(QueryBooksRequest) returns (stream Book) {}
rpc LogReadPages(stream PageRead) returns (google.protobuf.Empty) {}
rpc ListenForBooks(stream QueryBooksRequest) returns (stream Book) {}

Usage with NodeJS

Refer to grpc-web-node-http-transport.

All Docs

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.15.081,769latest
0.8.01,240beta

Version History

VersionDownloads (Last 7 Days)Published
0.15.081,769
0.14.153,022
0.14.05,690
0.13.012,647
0.12.020,806
0.11.0169
0.10.013
0.9.63,065
0.9.54
0.9.41
0.9.31
0.9.21
0.9.1122
0.8.01,240

Package Sidebar

Install

npm i @improbable-eng/grpc-web

Weekly Downloads

178,550

Version

0.15.0

License

Apache-2.0

Unpacked Size

65.7 kB

Total Files

22

Last publish

Collaborators

  • jonnyreeves
  • marcuslongmuir