https://github.com/syed007hassan/grpc-node-ts-example
Learn about gRPC and create a strongly typed application with gRPC communication using Node and Typescript - https://betterprogramming.pub/how-to-use-grpc-with-node-14e073aa1c84
https://github.com/syed007hassan/grpc-node-ts-example
auth grpc nodejs typescript
Last synced: 5 months ago
JSON representation
Learn about gRPC and create a strongly typed application with gRPC communication using Node and Typescript - https://betterprogramming.pub/how-to-use-grpc-with-node-14e073aa1c84
- Host: GitHub
- URL: https://github.com/syed007hassan/grpc-node-ts-example
- Owner: Syed007Hassan
- Created: 2023-11-13T13:39:40.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-13T14:53:25.000Z (almost 2 years ago)
- Last Synced: 2025-02-17T21:46:33.323Z (8 months ago)
- Topics: auth, grpc, nodejs, typescript
- Language: TypeScript
- Homepage: https://aws.amazon.com/compare/the-difference-between-rpc-and-rest/
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# grpc-node-typescript-example
A simple Node application with gRPC communication. Written with Typescript to ensure correct typing in client and server. For simplicity purpose there is only one function `login` shared between client and server.
The gRPC client and server communicate over a network using the gRPC protocol, which is built on top of HTTP/2. The client makes a remote procedure call (RPC) to the server by calling a method on a client-side stub. This method corresponds to a method defined in the server's service definition (the `auth.proto` file). The client and server use Protocol Buffers (protobuf) to serialize and deserialize the method's input and output data.Here's a step-by-step breakdown:
1. The client creates a `LoginRequest` object and calls the `login` method on the `AuthServiceClient` stub, passing in the `LoginRequest` and a callback function.
2. The `AuthServiceClient` serializes the `LoginRequest` to a protobuf message and sends it to the server over a HTTP/2 connection.
3. The server receives the protobuf message, deserializes it back into a `LoginRequest` object, and calls the `login` function with this object.
4. The `login` function checks the `LoginRequest` against its list of users, creates a `LoginResult`, and calls the callback function with this result.
5. The server serializes the `LoginResult` to a protobuf message and sends it back to the client over the HTTP/2 connection.
6. The client receives the protobuf message, deserializes it back into a `LoginResult` object, and calls the callback function with this object.
gRPC has several advantages over traditional RESTful APIs:
- **Performance**: gRPC uses HTTP/2, which is faster and more efficient than HTTP/1.1 used by most RESTful APIs. HTTP/2 supports multiplexing, bidirectional streaming, and flow control.
- **Strongly-typed interfaces**: gRPC uses protobuf, which enforces a strong typing system. This can help catch bugs at compile time that would only be caught at runtime with a RESTful API.
- **Bi-directional streaming**: gRPC supports streaming requests and responses, while REST is typically request/response.
- **Language agnostic**: gRPC supports many different languages, making it easy to create microservices in any language that can interoperate with each other.
- **Tooling**: gRPC provides tools like `protoc` to automatically generate client and server code from service definitions, reducing the amount of boilerplate code you have to write.
## Running server
```
npm run server
```## Running client
```
npm run client
```## Compiling proto file
After any change in `auth.proto` file it needs to be recompiled by running the following command:
```
protoc --plugin=protoc-gen-ts_proto=.\node_modules\.bin\protoc-gen-ts_proto.cmd --ts_proto_out=. ./protos/auth.proto --ts_proto_opt=outputServices=grpc-js,env=node,esModuleInterop=true
```