Server Side API
Retrieve the server
instance by calling loader.initServer()
. The API details are outlined below.
Server
add()
server.add(name, implementation, options)
Description:
- Binds the
proto service rpc
definition with the corresponding execution implementation.
Parameters:
Option | Type | Description |
---|---|---|
name | String | Required, service name including package name |
implementation | Object | Required, object or class containing methods defined in service rpc |
options | Object | Optional, { exclude, inherit } |
The details for the options
parameter are as follows:
Option | Type | Description |
---|---|---|
exclude | String[] | Optional, methods in `implementation` to be excluded from `service rpc` binding |
inherit | Class | Optional, if the `implementation` obtains `func` through inheritance, include the parent class in this parameter |
Returns:
void
: Empty.
remove()
server.remove(name)
Description:
- Unbinds an already bound
service
.
Parameters:
Option | Type | Description |
---|---|---|
name | String | Required, service name including package name |
Returns:
void
: Empty.
inject()
server.inject(customService)
Description:
- Injects a custom
service
into the currentserver
. Currently, only supports injection fromloader.initReflection()
.
use()
server.use(fn)
server.use([fn, fn1])
server.use(fn, fn1, fn2)
Description:
- Adds middleware for handling logic before and after rpc calls.
Parameters:
Option | Type | Description |
---|---|---|
single | MiddlewareFunction | Middleware function, usually defined as `async (ctx, next) => { await next() } |
array | MiddlewareFunction[] | Array of middleware functions |
mutli | ...MiddlewareFunction | Multiple middleware functions |
Returns:
void
: Empty.
listen()
await server.listen(addr, credentials)
Description:
- Listens on the specified address and port, starting the gRPC service.
Parameters:
Option | Type | Description |
---|---|---|
addr | String / { host, port } | Required, in the format of ip/domain + port, or { host, port } format |
credentials | ServerCredentials | Optional, certificate chain, obtained using `loader.makeServerCredentials()` |
Returns:
void
: Empty.
shutdown()
await server.shutdown()
Description:
- Gracefully shuts down the gRPC service.
Parameters:
void
: Empty.
Returns:
void
: Empty.
forceShutdown()
server.forceShutdown()
Description:
- Forcefully shuts down the gRPC service.
Parameters:
void
: Empty.
method[callProxy]
unary call
async unaryMethod(call) {}
sendMetadata()
const metadata = call.metadata.clone()
metadata.add(keyString, value)
call.sendMetadata(metadata)
Description:
call.metadata.clone()
: Receivesmetadata
from the client and clones it;metadata.add(keyString, value)
: Adds key-value pairs tometadata
;call.sendMetadata(metadata)
: Sendsmetadata
from the server to the client;
client stream call
async clientStreamMethod(call) {}
readAll()
for await (const data of call.readAll()) {}
Description:
- Receives a stream of information from the client.
Parameters:
void
: Empty.
Returns:
asyncIterator
: Returns anasyncIterator
object. We need to usefor await
to retrieve eachstream message
result;
end()
call.end()
Description:
- Ends processing of stream information on the server side and sends it to the client.
Parameters:
void
: Empty.
Returns:
void
: Empty.
server stream call
async serverStreamMethod(call) {}
write()
call.write(request)
Description:
- Sends a single stream of information from the server.
Parameters:
Option | Type | Description |
---|---|---|
request | Object | Optional, an object containing request parameters. The structure of the request object depends on the message type defined in the `.proto` file of the gRPC service |
Returns:
Boolean
: Boolean value, generallytrue
, indicating whether the sending was successful;
writeAll()
call.writeAll([ request ])
Description:
- Sends multiple streams of information from the server.
Parameters:
Option | Type | Description |
---|---|---|
request | Object | Optional, an object containing request parameters. The structure of the request object depends on the message type defined in the `.proto` file of the gRPC service |
Returns:
void
: Empty.
end()
call.end()
Description:
- Ends processing of stream information on the server side and sends it to the client.
Parameters:
void
: Empty.
Returns:
void
: Empty.
duplex stream call
async duplexStreamMethod(call) {}
write()
call.write(request)
Description:
- Sends a single stream of information from the server.
Parameters:
Option | Type | Description |
---|---|---|
request | Object | Optional, an object containing request parameters. The structure of the request object depends on the message type defined in the `.proto` file of the gRPC service |
Returns:
Boolean
: Boolean value, generallytrue
, indicating whether the sending was successful;
writeAll()
call.writeAll([ request ])
Description:
- Sends multiple streams of information from the server.
Parameters:
Option | Type | Description |
---|---|---|
request | Object | Optional, an object containing request parameters. The structure of the request object depends on the message type defined in the `.proto` file of the gRPC service |
Returns:
void
: Empty.
readAll()
for await (const data of call.readAll()) {}
Description:
- Receives a stream of information from the client.
Parameters:
void
: Empty.
Returns:
asyncIterator
: Returns anasyncIterator
object. We need to usefor await
to retrieve eachstream message
result;
end()
call.end()
Description:
- Ends processing of stream information on the server side and sends it to the client.
Parameters:
void
: Empty.
Returns:
void
: Empty.