How to use grpc-web - 10 common examples

To help you get started, we’ve selected a few grpc-web examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github berty / berty / client / packages / bridge / rpc / rpc.grpcweb.js View on Github external
let [host, port] = ['127.0.0.1', '8989']
if (window && window.location && window.location.href) {
  const url = new URL(window && window.location ? window.location.href : '')
  host =
    url.searchParams.get('daemon-host') ||
    url.searchParams.get('host') ||
    '127.0.0.1'
  port =
    url.searchParams.get('daemon-port') ||
    url.searchParams.get('port') ||
    '8989'
}
export const DefautlHostname = `http://${host}:${port}`

const { MethodInfo } = grpc.AbstractClientBase
const lazyMethod = payload => payload
const unary = (client, hostname) => async (method, request, metadata) => {
  const url = `${hostname}/${getServiceName(method)}/${method.name}`
  // const url = `${hostname}`
  const methodInfo = new MethodInfo(
    method.resolvedResponseType,
    lazyMethod,
    lazyMethod
  )

  const unary = client.unaryCall(url, request, metadata || {}, methodInfo)
  return unary
}

const stream = (client, hostname) => async (method, request, metadata) => {
  const url = `${hostname}/${getServiceName(method)}/${method.name}`
github pipe-cd / pipe / pkg / app / web / src / modules / application-counts.ts View on Github external
const res = await getApplicationCount().catch((e: { code: number }) => {
      // NOT_FOUND is the initial normal state, so it is excluded from error handling.
      if (e.code !== StatusCode.NOT_FOUND) {
        throw e;
      }
    });
github johanbrandhorst / grpc-web-compatibility-test / frontend / grpcweb / src / index.ts View on Github external
srv.on("status", function(status: grpcWeb.Status) {
    if (status.code != grpcWeb.StatusCode.OK) {
      console.log("Got streaming error");
      console.log("Error:", status.details);
      console.log("Code:", status.code);
      console.log("Metadata:", status.metadata);
    }
  });
}
github johanbrandhorst / grpc-web-compatibility-test / frontend / grpcWebText / proto / EchoServiceClientPb.ts View on Github external
options_: null | { [index: string]: string; };

  constructor (hostname: string,
               credentials?: null | { [index: string]: string; },
               options?: null | { [index: string]: string; }) {
    if (!options) options = {};
    if (!credentials) credentials = {};
    options['format'] = 'text';

    this.client_ = new grpcWeb.GrpcWebClientBase(options);
    this.hostname_ = hostname;
    this.credentials_ = credentials;
    this.options_ = options;
  }

  methodInfoEcho = new grpcWeb.AbstractClientBase.MethodInfo(
    EchoResponse,
    (request: EchoRequest) => {
      return request.serializeBinary();
    },
    EchoResponse.deserializeBinary
  );

  echo(
    request: EchoRequest,
    metadata: grpcWeb.Metadata | null,
    callback: (err: grpcWeb.Error,
               response: EchoResponse) => void) {
    return this.client_.rpcCall(
      this.hostname_ +
        '/grpc.gateway.testing.EchoService/Echo',
      request,
github johanbrandhorst / grpc-web-compatibility-test / frontend / grpcWeb / proto / EchoServiceClientPb.ts View on Github external
options_: null | { [index: string]: string; };

  constructor (hostname: string,
               credentials?: null | { [index: string]: string; },
               options?: null | { [index: string]: string; }) {
    if (!options) options = {};
    if (!credentials) credentials = {};
    options['format'] = 'binary';

    this.client_ = new grpcWeb.GrpcWebClientBase(options);
    this.hostname_ = hostname;
    this.credentials_ = credentials;
    this.options_ = options;
  }

  methodInfoEcho = new grpcWeb.AbstractClientBase.MethodInfo(
    EchoResponse,
    (request: EchoRequest) => {
      return request.serializeBinary();
    },
    EchoResponse.deserializeBinary
  );

  echo(
    request: EchoRequest,
    metadata: grpcWeb.Metadata | null,
    callback: (err: grpcWeb.Error,
               response: EchoResponse) => void) {
    return this.client_.rpcCall(
      this.hostname_ +
        '/grpc.gateway.testing.EchoService/Echo',
      request,
github johanbrandhorst / grpc-web-compatibility-test / frontend / grpcWeb / proto / EchoServiceClientPb.ts View on Github external
noOp(
    request: Empty,
    metadata: grpcWeb.Metadata | null,
    callback: (err: grpcWeb.Error,
               response: Empty) => void) {
    return this.client_.rpcCall(
      this.hostname_ +
        '/grpc.gateway.testing.EchoService/NoOp',
      request,
      metadata || {},
      this.methodInfoNoOp,
      callback);
  }

  methodInfoServerStreamingEcho = new grpcWeb.AbstractClientBase.MethodInfo(
    ServerStreamingEchoResponse,
    (request: ServerStreamingEchoRequest) => {
      return request.serializeBinary();
    },
    ServerStreamingEchoResponse.deserializeBinary
  );

  serverStreamingEcho(
    request: ServerStreamingEchoRequest,
    metadata?: grpcWeb.Metadata) {
    return this.client_.serverStreaming(
      this.hostname_ +
        '/grpc.gateway.testing.EchoService/ServerStreamingEcho',
      request,
      metadata || {},
      this.methodInfoServerStreamingEcho);
github johanbrandhorst / grpc-web-compatibility-test / frontend / grpcWebText / proto / EchoServiceClientPb.ts View on Github external
echoAbort(
    request: EchoRequest,
    metadata: grpcWeb.Metadata | null,
    callback: (err: grpcWeb.Error,
               response: EchoResponse) => void) {
    return this.client_.rpcCall(
      this.hostname_ +
        '/grpc.gateway.testing.EchoService/EchoAbort',
      request,
      metadata || {},
      this.methodInfoEchoAbort,
      callback);
  }

  methodInfoNoOp = new grpcWeb.AbstractClientBase.MethodInfo(
    Empty,
    (request: Empty) => {
      return request.serializeBinary();
    },
    Empty.deserializeBinary
  );

  noOp(
    request: Empty,
    metadata: grpcWeb.Metadata | null,
    callback: (err: grpcWeb.Error,
               response: Empty) => void) {
    return this.client_.rpcCall(
      this.hostname_ +
        '/grpc.gateway.testing.EchoService/NoOp',
      request,
github johanbrandhorst / grpc-web-compatibility-test / frontend / grpcWeb / proto / EchoServiceClientPb.ts View on Github external
constructor (hostname: string,
               credentials?: null | { [index: string]: string; },
               options?: null | { [index: string]: string; }) {
    if (!options) options = {};
    if (!credentials) credentials = {};
    options['format'] = 'binary';

    this.client_ = new grpcWeb.GrpcWebClientBase(options);
    this.hostname_ = hostname;
    this.credentials_ = credentials;
    this.options_ = options;
  }
github johanbrandhorst / grpc-web-compatibility-test / frontend / grpcWebText / proto / EchoServiceClientPb.ts View on Github external
constructor (hostname: string,
               credentials?: null | { [index: string]: string; },
               options?: null | { [index: string]: string; }) {
    if (!options) options = {};
    if (!credentials) credentials = {};
    options['format'] = 'text';

    this.client_ = new grpcWeb.GrpcWebClientBase(options);
    this.hostname_ = hostname;
    this.credentials_ = credentials;
    this.options_ = options;
  }
github nelhage / crossme / client / src / pb / CrossmeServiceClientPb.ts View on Github external
updateFill(
    request: UpdateFillArgs,
    metadata: grpcWeb.Metadata | null,
    callback: (err: grpcWeb.Error,
               response: UpdateFillResponse) => void) {
    return this.client_.rpcCall(
      this.hostname_ +
        '/crossme.CrossMe/UpdateFill',
      request,
      metadata || {},
      this.methodInfoUpdateFill,
      callback);
  }

  methodInfoSubscribe = new grpcWeb.AbstractClientBase.MethodInfo(
    SubscribeEvent,
    (request: SubscribeArgs) => {
      return request.serializeBinary();
    },
    SubscribeEvent.deserializeBinary
  );

  subscribe(
    request: SubscribeArgs,
    metadata?: grpcWeb.Metadata) {
    return this.client_.serverStreaming(
      this.hostname_ +
        '/crossme.CrossMe/Subscribe',
      request,
      metadata || {},
      this.methodInfoSubscribe);