This project demonstrates the basics of gRPC with Node.js, providing examples of different types of RPC methods and a practical Todo application.
gRPC is a high-performance, open-source Remote Procedure Call (RPC) framework developed by Google. It allows a client application to directly call methods on a server application as if it were a local object, making distributed computing more efficient.
- Protocol Buffers (Protobuf): Uses Protocol Buffers as the Interface Definition Language (IDL) for describing both the service interface and the structure of the payload messages.
- HTTP/2: Built on HTTP/2, which provides features like bidirectional streaming and multiplexing requests over a single connection.
- Multi-language Support: Supports multiple programming languages, making it ideal for microservices architectures.
- Streaming: Supports bidirectional streaming, allowing both client and server to send a sequence of messages.
- Strongly Typed: The use of Protocol Buffers ensures type safety and efficient serialization.
- Protocol Buffers (Protobuf): A language-neutral, platform-neutral, extensible mechanism for serializing structured data.
- Service Definition: Defined in a .proto file, specifies the methods that can be called remotely with their parameters and return types.
- Unary RPC: The client sends a single request and gets a single response back.
- Server Streaming RPC: The client sends a request to the server and gets a stream to read a sequence of messages back.
- Client Streaming RPC: The client writes a sequence of messages and sends them to the server, which reads them and returns a single response.
- Bidirectional Streaming RPC: Both sides send a sequence of messages using a read-write stream.
- Channel: A virtual connection to a gRPC server on a specified host and port.
- Stub: A client-side object that implements the same methods as the service.
- Node.js (v14 or later)
- npm
npm install
grpc-learning/
├── greeter/ # Greeter service module
│ ├── proto/ # Greeter proto definitions
│ │ └── greeter.proto # Greeter service definition
│ ├── server.js # Greeter server implementation
│ └── client.js # Greeter client implementation
├── todo/ # Todo service module
│ ├── proto/ # Todo proto definitions
│ │ └── todo.proto # Todo service definition
│ ├── server.js # Todo server implementation
│ └── client.js # Todo client implementation
├── grpc-express-integration.md # Guide for integrating gRPC with Express
├── package.json # Project dependencies and scripts
└── README.md # Project documentation
- Start the Greeter server:
npm run start:greeter-server
- In another terminal, run the Greeter client:
npm run start:greeter-client
- Start the Todo server:
npm run start:todo-server
- In another terminal, run the Todo client:
npm run start:todo-client
The client sends a single request and receives a single response.
rpc SayHello (HelloRequest) returns (HelloReply) {}
The client sends a single request and receives a stream of responses.
rpc SayHelloStreamReply (HelloRequest) returns (stream HelloReply) {}
The client sends a stream of requests and receives a single response.
rpc SayHelloStreamRequest (stream HelloRequest) returns (HelloReply) {}
Both client and server send streams of messages to each other.
rpc SayHelloBidirectional (stream HelloRequest) returns (stream HelloReply) {}
- Performance: gRPC is built on HTTP/2, which is more efficient than HTTP/1.1 used by most REST APIs.
- Strong Typing: Protocol Buffers provide strong typing, unlike JSON in REST.
- Code Generation: Automatic code generation for multiple languages.
- Streaming: Native support for streaming (server, client, and bidirectional).
- Deadline/Timeout: Built-in support for deadlines and timeouts.
- Interoperability: Works across multiple languages and platforms.
- Microservices communication
- Real-time communication with streaming
- Polyglot environments (multiple programming languages)
- Low-latency, high-throughput communication
- Point-to-point real-time communication