Basic Usage - AkashBabu/lib-grpc-client-pool GitHub Wiki

Changes to your .proto file for integrating this library with your application

All you methods declared in the services must start with an _(underscore) followed by an Uppercase letter For ex: _Hello

Request - Response (Simple Request for a simple Response)

The below code initializes pool connection with a MaxConns = 5 and the Prefix for gRPC methods would be RPC. Hence a method declared as _Hello would now be invoked as RPC_Hello

const client = new GRPCClient(PROTO_FILE_PATH, {
    maxConnections : 5,
    packageName    : 'Hello',
    serviceName    : 'Greeting',
    url            : 'localhost:50001',
    prefix         : 'RPC'
});

const { RPC_Hi, RPC_ReadStream, RPC_WriteStream, RPC_BothStream } = client;

const response = await RPC_Hi({msg: 'Hey Bot!'})

Request - Response(stream) (Simple request for a Stream of Responses)

const response = await RPC_ReadStream({msg: 'Hey Bot!'})
response.on('data', data => {
    console.log('msg from server:', data)
})
response.on('end', () => {
    console.log('Response stream ends')
})

Request(stream) - Response (Stream of Requests for a simple response)

const writer = await RPC_WriteStream({msg: 'Hey Bot!'}, (err, result) => {
    console.log('Response from server:', result);
});

writer.write({msg});
writer.end();

Request(stream) - Response(stream) (Stream of Requests for a stream of responses)

Tip: Use this when your requirement is to continuously get answers/responses from gRPC server for the queries. For ex: cookie/JWT to userId resolution service

const stream = await RPC_BothStream({msg: 'Hey Bot!'});

stream.on('data', data => {
    console.log('msg from server:', data)
})
stream.on('end', () => {
    console.log('Response stream ends')
})

stream.write({msg});
stream.end();