dev-resources.site
for different kinds of informations.
Building a gRPC Server with NestJS and Buf: A Comprehensive Showcase
Introduction
If you're familiar with NestJS and gRPC, you may have already referred to the official documentation on implementing gRPC in NestJS. While Protoc is a standard tool, as you delve deeper into customization, its complexity increases.
This is where Buf comes in. Buf offers a simpler alternative to Protoc for generating SDKs, documentation, and more. Despite its simplicity, I noticed a lack of sample projects or showcases integrating Buf with NestJS.
Therefore, I took it upon myself to create one.
Overview of repository
This repository (https://github.com/Ko1103/nestjs-buf-example) is a NestJS and Buf usage example for protobuf server implementation.
It will help you to understand about
- directory structure
- explains requirements
- installation process
- how to run the application
Project Structure
.
āāā Dockerfile
āāā README.md
āāā docker-compose.yml
āāā nest-cli.json
āāā proto // proto files
āāā src
ā āāā posts // User posts resource
ā āāā proto // TypeScript SDK generated from proto file
ā āāā users // Users resource
ā ā āāā users.controller.ts
ā ā āāā users.module.ts
ā ā āāā users.service.ts
ā āāā app.module.ts
ā āāā main.ts
āāā tsconfig.build.json
āāā tsconfig.json
Installation and Setup
Requirements
- docker & docker compose cli
- Node.js v18
Please note that I developed this project using an M1 Mac.
To get started, clone repository.
git clone [email protected]:Ko1103/nestjs-buf-example.git
Next, install the node_modules
npm ci
Generate TypeScript SDK from protobuf files
npm run proto:gen
Finally bring up docker container.
docker compose app
Try accessing gRPC via http:localhost/5000
.
Conclusion
With this repository, you now have the ability to create new gRPC projects with NestJS using Buf. However, I'm concerned that the TypeScript SDK generated into src/proto may not be optimal. Please feel free to share any alternative options or ideas you may have regarding this.
Thank you for reading until the end.
Reference
Featured ones: