Git Product home page Git Product logo

rxjs-grpc's Introduction

Build Status npm version

rxjs-grpc

Installation

$ npm install rxjs-grpc rxjs grpc

Quickstart

Create your protobuf definition file sample.proto:

syntax = "proto3";

package sample;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

Generate your TypeScript interfaces:

$ ./node_modules/.bin/rxjs-grpc -o grpc-namespaces.ts *.proto

Implement your typesafe server returning Observable<sample.HelloReply>:

import { of } from 'rxjs';
import { serverBuilder } from 'qp-rxjs-grpc';
import { sample } from './grpc-namespaces';

// Pass the path of proto file and the name of namespace
const server = serverBuilder<sample.ServerBuilder>('sample.proto', 'sample')
// Add implementation
server.addGreeter({
  sayHello(request: sample.HelloRequest) {
    return of({
      message: 'Hello ' + request.name
    });
  }
})
// Start the server to listen on port 50051
server.start('0.0.0.0:50051');

Call it from a client:

import { clientFactory } from 'qp-rxjs-grpc';
import { sample } from './grpc-namespaces';

// Pass the path of proto file and the name of namespace
const Services = clientFactory<sample.ClientFactory>('sample.proto', 'sample');
// Create a client connecting to the server
const services = new Services('localhost:50051');
// Get a client for the Greeter service
const greeter = services.getGreeter();

// Call the service by passing a sample.HelloRequest
greeter.sayHello({ name: 'world' }).forEach(response => {
  console.log(`Greeting: ${response.message}`);
});

Generated interfaces

import { Observable } from 'rxjs';

export namespace sample {

  export interface ClientFactory {
    getGreeter(): sample.Greeter;
  }

  export interface ServerBuilder {
    addGreeter(impl: sample.Greeter): sample.ServerBuilder;
  }

  export interface Greeter {
    sayHello(request: sample.HelloRequest): Observable<sample.HelloReply>;
  }

  export interface HelloRequest {
    name?: string;
  }

  export interface HelloReply {
    message?: string;
  }

}

Examples

You can see a simple example project in the examples folder.

rxjs-grpc's People

Contributors

kondi avatar woo-gim avatar gimdongwoo avatar eddy-jeon avatar slumbi avatar ksaldana1 avatar maikelh avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.