The @loopback/grpc
component enables LoopBack 4 as a gRPC Server. Also it provides with a gRPC decorator to define your RPC Method implementations from your Application Controllers.
Install the @loopback/grpc
component in your LoopBack 4 Application.
$ npm install --save @loopback/grpc
import {Application} from '@loopback/core';
import {GrpcComponent, GrpcConfig} from '@loopback/grpc';
import {MyGreeter} from './MyGreeter';
const config: GrpcConfig = {
proto: './file.proto',
package: 'myawesomepackage'
}
const app = new Application({
components: [GrpcComponent],
grpc: config
});
app.controller(MyGreeter});
await app.start();
The @loopback/grpc
component provides you with a handy decorator to implement GRPC Methods.
import {grpc} from '@loopback/grpc';
// You create the following types according your own proto.file
import {Greeter, HelloRequest, HelloReply} from './types';
/**
* @class MyGreeter
* @description Implements grpc proto service
**/
export class MyGreeter implements Greeter {
// Tell LoopBack that this is a Service RPC implementation
@grpc()
SayHello(request: HelloRequest): HelloReply {
const reply: HelloReply = {message: 'Hello ' + request.name};
return reply;
}
}
Get started by either downloading this project or cloning it as follows:
$ git clone https://github.com/strongloop/loopback4-extension-grpc.git
$ cd loopback4-extension-grpc && npm install
run npm test
from the root folder.
See all contributors.
MIT