@forgeframework/grpc-client
v0.3.0
Published
gRPC client for the Forge Framework.
Maintainers
Readme
@forgeframework/grpc-client
gRPC client for the Forge Framework.
Part of the Forge Framework — a TypeScript framework for backend microservices and web applications.
Installation
npm install @forgeframework/grpc-clientUsage
import { GrpcClientFactory, GrpcStatusCode } from '@forgeframework/grpc-client';
const factory = new GrpcClientFactory();
const client = factory.createFromProto('./protos/user-service.proto', {
host: 'localhost',
port: 50051,
credentials: { type: 'insecure' },
retry: {
maxAttempts: 3,
retryableStatusCodes: [GrpcStatusCode.UNAVAILABLE, GrpcStatusCode.INTERNAL],
backoff: { initial: 100, max: 5000, multiplier: 2 },
},
pool: {
size: 4,
loadBalancing: 'round-robin',
},
defaultDeadline: 10000,
});
interface User {
id: string;
name: string;
email: string;
}
const user = await client.unary<{ id: string }, User>(
'UserService/GetUser',
{ id: 'user-123' },
{ deadline: 5000, metadata: { 'x-correlation-id': 'abc-123' } },
);
const stream = client.serverStream<{ query: string }, User>(
'UserService/SearchUsers',
{ query: 'forge' },
);
for await (const result of stream) {
console.log(`Found user: ${result.name}`);
}
const upload = client.clientStream<DataChunk, UploadResult>(
'FileService/Upload',
);
for (const chunk of chunks) {
upload.write(chunk);
}
upload.end();
const uploadResult = await upload.response;
const chat = client.bidiStream<ChatMessage, ChatMessage>(
'ChatService/Chat',
);
chat.write({ text: 'Hello', sender: 'client' });
(async () => {
for await (const reply of chat) {
console.log(`Server: ${reply.text}`);
}
})();
chat.write({ text: 'How are you?', sender: 'client' });
chat.end();
await client.close();Documentation
Full API reference and guides: https://carbonforge.io/framework/docs/clients/grpc-client
(Documentation site launching soon.)
License
MIT © Carbon Forge
