Client TS
Let's break down the code step by step:
import path from "path";
import * as grpc from "@grpc/grpc-js";
import * as protoLoader from "@grpc/proto-loader";
import { ProtoGrpcType } from "./proto/random";
import { PingRequet } from "./proto/randomPackage/PingRequet";
import { PongResponse } from "./proto/randomPackage/PongResponse";
In this section, the script imports necessary modules and types:
- path: The path module is used for working with file and directory paths.
- grpc and protoLoader from @grpc/grpc-js: These are modules provided by the @grpc/grpc-js library for creating a gRPC client and loading ProtoBuf definitions.
- ProtoGrpcType: This is a custom type imported from ProtoBuf-generated files. It represents the types of gRPC services and messages.
- PingRequest and PongResponse: These are custom message types imported from ProtoBuf-generated files. They represent the request and response message types for the PingPong gRPC method.
const packageDef = protoLoader.loadSync(
path.resolve(__dirname, "./proto/random.proto")
);
const grpcObj = grpc.loadPackageDefinition(packageDef) as unknown as ProtoGrpcType;
In this section, the script loads the ProtoBuf definition file (random.proto) using protoLoader.loadSync. This definition file defines the structure of the gRPC service and its methods. It then loads the package definition using grpc.loadPackageDefinition and casts it to the custom ProtoGrpcType type, which represents the gRPC service and message types.
const client = new grpcObj.randomPackage.Random(
"0.0.0.0:9000",
grpc.credentials.createInsecure()
);
Here, the script creates a gRPC client instance by instantiating grpcObj.randomPackage.Random. It specifies the target server address as "0.0.0.0:9000" and uses insecure credentials (grpc.credentials.createInsecure()) for communication. In a production environment, you would typically use secure credentials for secure communication.
const deadline: Date = new Date();
deadline.setSeconds(deadline.getSeconds() + 5);
client.waitForReady(deadline, (err: Error | undefined) => {
if (err) {
console.error(err);
return;
}
onClientReady();
});
In this section, the script sets a deadline for the gRPC client. It creates a Date object and sets it to 5 seconds in the future. Then, it calls client.waitForReady to wait for the gRPC client to become ready before making requests. If there is an error during the wait, it logs the error. If the client becomes ready, it calls the onClientReady function.
function onClientReady() {
const request: PingRequet = { message: "Ping" };
client.PingPong(
request,
(err: grpc.ServerErrorResponse | null, response: PongResponse | undefined) => {
if (err) {
console.error(err);
return;
}
console.log(response);
}
);
}
The onClientReady function is called when the gRPC client is ready. It creates a PingRequest object with a message field set to "Ping" and then makes a gRPC request to the server using client.PingPong. When the response is received, it logs the response. If there's an error, it logs the error.
In summary, this code sets up a gRPC client in TypeScript, loads the gRPC service definition from a ProtoBuf file, and makes a PingPong gRPC request to a server at "0.0.0.0:9000" after ensuring that the client is ready. It demonstrates the basic structure of a gRPC client in a TypeScript environment.