Skip to content
Documentation
Client Side

Client Side API

Retrieve the clients instance by using loader.initClients(). The API details are outlined below.

Clients

get()

const client = clients.get(name, options)

Description:

  • Obtain an instance of the service client corresponding to the proto definition of the provided name. This client uses a proxy and caches the instance.

Parameters:

OptionTypeDescription
nameStringRequired, the service name including the package name
optionsObjectOptional, { url, channelOptions, credentials, timeout }

Details of the options parameter:

OptionTypeDescription
urlString / { host, port }Optional, in the format of ip/domain + port, or { host, port }
channelOptionsChannelOptionsOptional, proto channel options, default to using built-in channelOptions
credentialsChannelCredentialsOptional, certificate chain, obtained using `loader.makeClientCredentials()`
timeoutNumberOptional, in milliseconds, controlling the timeout for rpc calls

Returns:

  • Returns an object with the already proxy method defined in the service proto.

getReal()

const client = clients.getReal(name, options)

Description:

  • Obtain an instance of the service client corresponding to the proto definition of the provided name. This client does not use a proxy and does not cache the instance.

Parameters:

OptionTypeDescription
nameStringRequired, the service name including the package name
optionsObjectOptional, { url, channelOptions, credentials, timeout }
OptionTypeDescription
urlString / { host, port }Optional, in the format of ip/domain + port, or { host, port }
channelOptionsChannelOptionsOptional, proto channel options, default to using built-in channelOptions
credentialsChannelCredentialsOptional, certificate chain, obtained using `loader.makeClientCredentials()`
timeoutNumberOptional, in milliseconds, controlling the timeout for rpc calls

Returns:

  • Returns an object with the callback method defined in the service proto.

use()

clients.use(fn)
clients.use([fn, fn1])
clients.use(fn, fn1, fn2)

Description:

  • Add middleware to handle logic before and after the rpc call.

Parameters:

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

Returns:

  • void: Empty.

clear()

clients.clear(fn)

Description:

  • Clear all caches of the client.

Returns:

  • void: Empty.

init()

clients.init(options)

Description:

  • Reinitialization requires the prior execution of clear() to take effect.

Parameters:

OptionTypeDescription
servicesObjectRequired, service and address
channelOptionsChannelOptionsOptional, proto channel options, default uses built-in channelOptions
credentialsChannelCredentialsOptional, certificate chain, use `loader.makeCredentials()` to get`
OptionTypeDescription
services.keyStringservice name, including the complete package name, e.g., 'test.helloworld.Greeter'
services.valueStringip/domain + port format, e.g., '192.168.1.33:5001'
services.valueObject{ host, port } format, `host`: string, supports IP and domain; `port`: number, minimum value is 0, maximum value is 65535

Returns:

  • void: Empty.

proxy[method]

unary call

const { status, peer, metadata, response } = await client.rpcUnaryMethod(request, metadata, options)

Description:

  • Make a rpcUnaryMethod call, point-to-point, and wait for the result to be returned.

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
metadataMetadataOptional, used to pass metadata related to the request. Metadata is represented as key-value pairs and can include information for authentication, authorization, tracking, or other purposes
optionsObjectOptional, used to set specific call options. It is an object containing a set of key-value pairs that can be used to configure the behavior of the call. Some available options include timeout, retry strategy, etc. If not passed, the default timeout is set to 10 seconds and can be modified by passing `timeout`

Returns:

OptionTypeDescription
statusStatusObjectStatus after server processing
peerStringAddress information initiated by the client
metadataMetadataMetadata information returned by the server
responseObjectResult returned after server processing

client stream

const clientStreamCall = await client.rpcClientStreamMethod(metadata, options)

Description:

  • Handle a rpcClientStreamMethod call, providing the ability for the client to send stream information and receive the result of server processing the stream.

Parameters:

OptionTypeDescription
metadataMetadataOptional, used to pass metadata related to the request. Metadata is represented as key-value pairs and can include information for authentication, authorization, tracking, or other purposes
optionsObjectOptional, used to set specific call options. It is an object containing a set of key-value pairs that can be used to configure the behavior of the call. Some available options include timeout, retry strategy, etc. If not passed, the default timeout is set to 10 seconds and can be modified by passing `timeout`

Returns:

  • client stream call instance, providing a series of methods to handle client streams.

write()

clientStreamCall.write(request)

Description:

  • Client sends a stream message.

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

writeAll()

clientStreamCall.writeAll([request])

Description:

  • Client batch sends multiple stream messages.

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.

writeEnd()

const { status, peer, metadata, response } = await clientStreamCall.writeEnd()

Description:

  • Get the result of server processing the stream information.

Returns:

OptionTypeDescription
statusStatusObjectStatus after server processing
peerStringAddress information initiated by the client
metadataMetadataMetadata information returned by the server
responseObjectResult returned after server processing

server stream

const serverStreamCall = await client.rpcServerStreamMethod(request, metadata, options)

Description:

  • Handle a rpcServerStreamMethod call, providing the ability for the client to initiate a call and receive stream information sent by 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
metadataMetadataOptional, used to pass metadata related to the request. Metadata is represented as key-value pairs and can include information for authentication, authorization, tracking, or other purposes
optionsObjectOptional, used to set specific call options. It is an object containing a set of key-value pairs that can be used to configure the behavior of the call. Some available options include timeout, retry strategy, etc. If not passed, the default timeout is set to 10 seconds and can be modified by passing `timeout`

Returns:

  • server stream call instance, providing a series of methods to handle client streams.

readAll()

const asyncIterator = await serverStreamCall.readAll()

Description:

  • Receive stream information sent by the server.

Returns:

  • asyncIterator: An asyncIterator object returned by the server, and we need to use for await to get the results of each stream message sent by the server;

readEnd()

const { status, peer, metadata } = await serverStreamCall.readEnd()

Description:

  • Receive status, peer, and metadata information after server processing.

Returns:

OptionTypeDescription
statusStatusObjectStatus after server processing
peerStringAddress information initiated by the client
metadataMetadataMetadata information returned by the server

duplex stream

const clientDuplexStreamCall = client.rpcDuplexStreamMethod(metadata, options)

Description:

  • Handle a rpcDuplexStreamMethod call, providing the ability for the client to send stream information and receive the server’s stream information.

Parameters:

OptionTypeDescription
metadataMetadataOptional, used to pass metadata related to the request. Metadata is represented as key-value pairs and can include information for authentication, authorization, tracking, or other purposes
optionsObjectOptional, used to set specific call options. It is an object containing a set of key-value pairs that can be used to configure the behavior of the call. Some available options include timeout, retry strategy, etc. If not passed, the default timeout is set to 10 seconds and can be modified by passing `timeout`

Returns:

  • duplex stream call instance, providing a series of methods to handle client streams.

write()

clientDuplexStreamCall.write(request)

Description:

  • Client sends a stream message.

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

writeAll()

clientDuplexStreamCall.writeAll([request])

Description:

  • Client batch sends multiple stream messages.

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.

writeEnd()

clientDuplexStreamCall.writeEnd()

Description:

  • Signal to the server that the client has finished sending stream information.

Returns:

  • void: Empty.

readAll()

const asyncIterator = await clientDuplexStreamCall.readAll()

Description:

  • Receive stream information sent by the server.

Returns:

  • asyncIterator: An asyncIterator object returned by the server, and we need to use for await to get the results of each stream message sent by the server;

readEnd()

const { status, peer, metadata } = clientDuplexStreamCall.readEnd()

Description:

  • Receive the status and metadata information returned by the server.

Returns:

OptionTypeDescription
statusStatusObjectStatus after server processing
peerStringAddress information initiated by the client
metadataMetadataMetadata information returned by the server

proxy.call[method]

const callInstance = client.call.rpcMethod(request, metadata, options, callback)

Description:

  • Handle a call.rpcMethod call using a callback function and event listening mechanism to process server information.

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
metadataMetadataOptional, used to pass metadata related to the request. Metadata is represented as key-value pairs and can include information for authentication, authorization, tracking, or other purposes
optionsObjectOptional, used to set specific call options. It is an object containing a set of key-value pairs that can be used to configure the behavior of the call. Some available options include timeout, retry strategy, etc. If not passed, the default timeout is set to 10 seconds and can be modified by passing `timeout`
callbackFunctionRequired, callback function to receive and process server errors and results

Returns:

  • call instance, providing a series of methods to handle the client.

on()

callInstance.on('eventName', callback)

Description:

  • Listen for server events and process the listening data.

Parameters:

OptionTypeDescription
eventNameStringRequired, event name, including `data`, `end`, `status`, `metadata`, and `error`
callbackFunctionRequired, callback function to receive and process server errors and results

Returns:

  • void: Empty.

write()

callInstance.write(request)

Description:

  • Client sends a stream message.

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

end()

callInstance.end()

Description:

  • Signal to the server that the client has finished sending

Parameters:

  • void: Empty.

Returns:

  • void: Empty.