Skip to content

denishrana09/grpc-nodejs-demo

Repository files navigation

gRPC Learning Project

This project demonstrates the basics of gRPC with Node.js, providing examples of different types of RPC methods and a practical Todo application.

What is gRPC?

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.

Key Features of gRPC

  • 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.

gRPC Glossary

  • 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.

Prerequisites

  • Node.js (v14 or later)
  • npm

Installation

npm install

Project Structure

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

Running the Greeter Demo

  1. Start the Greeter server:
npm run start:greeter-server
  1. In another terminal, run the Greeter client:
npm run start:greeter-client

Running the Todo App Demo

  1. Start the Todo server:
npm run start:todo-server
  1. In another terminal, run the Todo client:
npm run start:todo-client

Screenshot of the Todo App Demo

Todo App Demo

Types of RPC Methods Demonstrated

1. Unary RPC (Simple Request-Response)

The client sends a single request and receives a single response.

rpc SayHello (HelloRequest) returns (HelloReply) {}

2. Server Streaming RPC

The client sends a single request and receives a stream of responses.

rpc SayHelloStreamReply (HelloRequest) returns (stream HelloReply) {}

3. Client Streaming RPC

The client sends a stream of requests and receives a single response.

rpc SayHelloStreamRequest (stream HelloRequest) returns (HelloReply) {}

4. Bidirectional Streaming RPC

Both client and server send streams of messages to each other.

rpc SayHelloBidirectional (stream HelloRequest) returns (stream HelloReply) {}

Benefits of gRPC Over REST

  1. Performance: gRPC is built on HTTP/2, which is more efficient than HTTP/1.1 used by most REST APIs.
  2. Strong Typing: Protocol Buffers provide strong typing, unlike JSON in REST.
  3. Code Generation: Automatic code generation for multiple languages.
  4. Streaming: Native support for streaming (server, client, and bidirectional).
  5. Deadline/Timeout: Built-in support for deadlines and timeouts.
  6. Interoperability: Works across multiple languages and platforms.

When to Use gRPC

  • Microservices communication
  • Real-time communication with streaming
  • Polyglot environments (multiple programming languages)
  • Low-latency, high-throughput communication
  • Point-to-point real-time communication

Resources for Learning More

About

Contains two gRPC examples using nodejs. one is simple greeter service and another is todo service.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published