Examples
HelloWorld example
In this example, we will try to define a basic service using proto buffers, register it using the service client and calling it by its route.
Proto file definition:
syntax="proto3";
package my.package.v1;
service HelloWorld {
rpc addOne(Request) returns (Response) {}
}
message Request {
int32 value = 1;
}
message Response {
int32 value = 1;
}
Callee (server):
const path = require('path')
const { ServiceClient } = require('hera-grpc')
const sc = new ServiceClient({
host: 'localhost',
port: 50051,
zk: 'zk://localhost:2181/hera-test',
})
sc.registerService({
routes: '/example-1.0.0/dev~service_route/helloworld',
serviceName: 'my.package.v1.HelloWorld',
filename: path.join(__dirname, 'helloworld.proto'),
handlers: {
addOne: (call, callback) => {
const { request } = call
callback(null, { value: request.value + 1 })
},
},
})
sc.on('error', (error) => {
console.error('Error from service client:', error)
})
try {
sc.connect()
} catch (error) {
console.error('Callee error:', error)
sc.close()
}
this will register the HelloWorld
service for route /example-1.0.0/dev~service_route/helloworld
and start the grpc server on localhost:50051
.
The service client will emit registered
event when it's ready to handle requests - for more details check events.
Caller (client):
Client can now call the service by its route /example-1.0.0/dev~service_route/helloworld
:
const { assert } = require('console')
const promisify = require('util').promisify
const { ServiceClient } = require('hera-grpc')
const sc = new ServiceClient({
zk: 'zk://localhost:2181/hera-test',
})
sc.once('connected', async () => {
// Get stub with the rpc methods for the specified route
const stub = await sc.getStub('/example-1.0.0/dev~service_route/helloworld')
stub.addOne = promisify(stub.addOne)
const request = { value: 41 }
const response = await stub.addOne(request)
assert(response.value === 42)
stub.close()
sc.close()
})
sc.once('error', (error) => {
process.exitCode = 1
console.error('Error from service client:', error)
sc.close()
})
try {
sc.connect()
} catch (error) {
console.error('Caller error:', error)
sc.close()
}
POI service example
In this example, we will try to register and call the demo service from the Node gRPC Basics tutorial:
Callee (server):
const path = require('path')
const { ServiceClient } = require('hera-grpc')
const poiProtoPath = path.join(__dirname, '../proto/poi/poi.proto')
// Handlers from the POI service tutorial
const poiHandlers = require('./poi/handlers')
const sc = new ServiceClient({
host: 'localhost',
port: 50051,
zk: 'zk://localhost:2181/hera-test',
})
sc.registerService({
routes: '/example-1.0.0/dev~service_route/poi',
serviceName: 'RouteGuide',
handlers: poiHandlers,
filename: poiProtoPath,
loadOptions: {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
},
})
sc.on('error', (error) => {
console.error('Error from service client:', error)
})
try {
sc.connect()
} catch (error) {
console.error('Callee error:', error)
sc.close()
}
this will register the poi service for route /example-1.0.0/dev~service_route/poi
.
Caller (client):
const { ServiceClient } = require('hera-grpc')
const poiClient = require('./poi/client')
const sc = new ServiceClient({
zk: 'zk://localhost:2181/hera-test',
})
sc.once('connected', async () => {
// Get stub with the rpc methods for the specified route
const stub = await sc.getStub('/example-1.0.0/dev~service_route/poi')
// Client from the tutorial
await poiClient(stub)
stub.close()
sc.close()
})
sc.once('error', (error) => {
process.exitCode = 1
console.error('Error from service client:', error)
sc.close()
})
try {
sc.connect()
} catch (error) {
console.error('Caller error:', error)
sc.close()
}