https://github.com/kondi/rxjs-grpc
Typesafe gRPC with RxJS in TypeScript
https://github.com/kondi/rxjs-grpc
grpc rxjs typescript
Last synced: 28 days ago
JSON representation
Typesafe gRPC with RxJS in TypeScript
- Host: GitHub
- URL: https://github.com/kondi/rxjs-grpc
- Owner: kondi
- License: mit
- Created: 2017-03-03T19:22:33.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T15:43:15.000Z (over 2 years ago)
- Last Synced: 2024-04-14T19:20:44.640Z (about 1 year ago)
- Topics: grpc, rxjs, typescript
- Language: TypeScript
- Size: 465 KB
- Stars: 202
- Watchers: 10
- Forks: 41
- Open Issues: 28
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/kondi/rxjs-grpc)
[](https://badge.fury.io/js/rxjs-grpc)# rxjs-grpc
## Installation
```sh
$ npm install rxjs-grpc rxjs grpc
```## Quickstart
Create your protobuf definition file `sample.proto`:
```protobuf
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:
```sh
$ ./node_modules/.bin/rxjs-grpc -o grpc-namespaces.ts *.proto
```Implement your typesafe server returning `Observable`:
```typescript
import { of } from 'rxjs';
import { serverBuilder } from 'rxjs-grpc';
import { sample } from './grpc-namespaces';// Pass the path of proto file and the name of namespace
const server = 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:
```typescript
import { clientFactory } from 'rxjs-grpc';
import { sample } from './grpc-namespaces';// Pass the path of proto file and the name of namespace
const Services = 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
```typescript
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;
}export interface HelloRequest {
name?: string;
}export interface HelloReply {
message?: string;
}}
```## Examples
You can see a simple example project in the [examples folder](examples).