grpc.unary
allows making unary gRPC requests.
There is also grpc.invoke
for making server-streaming requests and grpc.client
for making bi-directional requests that will both work with unary methods.
grpc.unary(methodDescriptor: MethodDescriptor, props: UnaryRpcOptions): Request;
methodDescriptor
is a generated method definition (see code generation for how to generate these).
host: string
- The server address (
"https://example.com:9100"
)
- The server address (
request: grpc.ProtobufMessage
- The single request message to send to the server
metadata: grpc.Metadata
- The metadata to send to the server
onEnd: (output: UnaryOutput<TResponse>) => void)
- A callback for the end of the request and trailers being received
transport?: TransportConstructor
- (optional) A function to build a
Transport
that will be used for the request. If no transport is specified then a browser-compatible transport will be used. See transport.
- (optional) A function to build a
debug?: boolean
- (optional) if
true
, debug information will be printed to the console
- (optional) if
status: Code
- The status code that the request ended with
statusMessage: string
- The status message that the request ended with
headers: Metadata
- The headers (Metadata) that the server sent
message: TResponse | null
- The single message that the server sent in the response.
trailers: Metadata
- The trailers (Metadata) that the server sent
// Close the connection to the server without waiting for any response
close(): void;
A unary gRPC request goes through the following stages:
- Request with optional metadata and a single message is sent to the server -
unary()
- Server sends headers (metadata)
- Server responds with one message to the client
- Server closes the request with status code and trailers (metadata) -
onEnd
callback called with the message and metadata that was received
const request = new QueryBooksRequest();
request.setAuthorPrefix("Geor");
const grpcRequest = grpc.unary(BookService.QueryBooks, {
host: "https://example.com:9100",
metadata: new grpc.Metadata({"HeaderTestKey1": "ClientValue1"}),
onEnd: (({status, statusMessage, headers, message, trailers: string, trailers: grpc.Metadata}) => {
console.log("onEnd", status, statusMessage, headers, message, trailers);
},
});
grpcRequest.close();// Included as an example of how to close the request, but this usage would cancel the request immediately