Rough implementation of rxified wrapper and tools for grpc-js lib.
// GreetingService.proto
syntax = "proto3";
package org.example;
service GreetingService {
rpc Greet(Request) returns (Response);
}
message Request {
string name = 1;
}
message Response {
string message = 1;
}
// server.js
class GreetingService {
Greet(call, callback) {
const name = call.request.name;
if (name)
callback(null, { message: `Hello, ${name}!` });
else
callback(new Error('Name is not defined.'));
}
}
const RxGrpc = require('@rough/rx-grpc');
new RxGrpc()
.withProtoFiles(__dirname + '/*.proto')
.serve('org.example.GreetingService', new GreetingService())
.startServer()
.subscribe(
started => console.log('Listening at grpc://0.0.0.0:50051 ...'),
err => console.log(err),
complete => {}
);
// client.js
const { flatMap } = require('rxjs/operators');
const RxGrpc = require('@rough/rx-grpc');
const rxgrpc = new RxGrpc().withProtoFiles(__dirname + '/*.proto');
const name = (process.argv.length > 2) ? process.argv[2] : null;
rxgrpc.service('org.example.GreetingService', 'localhost:50051').pipe(
flatMap(GreetingService => GreetingService.Greet({ name }))
)
.subscribe(
response => console.log(response.message),
err => console.log(err),
complete => {}
);