Skip to content
Documentation
Server Side

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:

OptionTypeDescription
nameStringRequired, service name including package name
implementationObjectRequired, object or class containing methods defined in service rpc
optionsObjectOptional, { exclude, inherit }

The details for the options parameter are as follows:

OptionTypeDescription
excludeString[]Optional, methods in `implementation` to be excluded from `service rpc` binding
inheritClassOptional, 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:

OptionTypeDescription
nameStringRequired, service name including package name

Returns:

  • void: Empty.

inject()

server.inject(customService)

Description:

  • Injects a custom service into the current server. Currently, only supports injection from loader.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:

OptionTypeDescription
singleMiddlewareFunctionMiddleware function, usually defined as `async (ctx, next) => { await next() }
arrayMiddlewareFunction[]Array of middleware functions
mutli...MiddlewareFunctionMultiple middleware functions

Returns:

  • void: Empty.

listen()

await server.listen(addr, credentials)

Description:

  • Listens on the specified address and port, starting the gRPC service.

Parameters:

OptionTypeDescription
addrString / { host, port }Required, in the format of ip/domain + port, or { host, port } format
credentialsServerCredentialsOptional, 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(): Receives metadata from the client and clones it;
  • metadata.add(keyString, value): Adds key-value pairs to metadata;
  • call.sendMetadata(metadata): Sends metadata 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 an asyncIterator object. We need to use for await to retrieve each stream 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:

OptionTypeDescription
requestObjectOptional, 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, generally true, indicating whether the sending was successful;

writeAll()

call.writeAll([ request ])

Description:

  • Sends multiple streams of information from the server.

Parameters:

OptionTypeDescription
requestObjectOptional, 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:

OptionTypeDescription
requestObjectOptional, 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, generally true, indicating whether the sending was successful;

writeAll()

call.writeAll([ request ])

Description:

  • Sends multiple streams of information from the server.

Parameters:

OptionTypeDescription
requestObjectOptional, 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 an asyncIterator object. We need to use for await to retrieve each stream 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.