Logo

dev-resources.site

for different kinds of informations.

Building a gRPC Server with NestJS and Buf: A Comprehensive Showcase

Published at
5/6/2024
Categories
typescript
nestjs
buf
protobuf
Author
ko1103
Categories
4 categories in total
typescript
open
nestjs
open
buf
open
protobuf
open
Author
6 person written this
ko1103
open
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Next, install the node_modules

npm ci
Enter fullscreen mode Exit fullscreen mode

Generate TypeScript SDK from protobuf files

npm run proto:gen
Enter fullscreen mode Exit fullscreen mode

Finally bring up docker container.

docker compose app
Enter fullscreen mode Exit fullscreen mode

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

protobuf Article's
30 articles in total
Favicon
Protocol Buffers as a Serialization Format
Favicon
Part 2: Defining the Authentication gRPC Interface
Favicon
Compile Protocol Buffers & gRPC to Typescript with Yarn
Favicon
Use RBAC to protect your gRPC service right on proto definition
Favicon
Gamechanger Protobuf
Favicon
Gamechanger Protobuf
Favicon
RPC Action EP2: Using Protobuf and Creating a Custom Plugin
Favicon
FauxRPC
Favicon
Why should we use Protobuf in Web API as data transfer protocol.
Favicon
JSON vs FlatBuffers vs Protocol Buffers
Favicon
gRPC - Unimplemented Error 12
Favicon
A protoc compiler plugin that generates useful extension code for Kotlin/JVM
Favicon
Reducing flyxc data usage
Favicon
Koinos, Smart Contracts, WASM & Protobuf
Favicon
This Week I Learnt: gRPC & Protobuf
Favicon
Building a gRPC Server with NestJS and Buf: A Comprehensive Showcase
Favicon
Exploring Alternatives: Are There Better Options Than JSON?
Favicon
Creating the Local First Stack
Favicon
Roll your own auth with Rust and Protobuf
Favicon
OCaml, Python and protobuf
Favicon
Introduction to Protocol Buffers
Favicon
Using Protobuf with TypeScript
Favicon
[Typia] I made Protocol Buffer library of TypeScript, easiest in the world
Favicon
Protoc Plugins with Go
Favicon
Using Azure Web PubSub with Protobuf subprotocol in .NET
Favicon
A secret weapon to improve the efficiency of golang development, a community backend service was developed in one day
Favicon
Linting Proto Files With Buf
Favicon
What is gRPC
Favicon
fast framework for binary serialization and deserialization in Java, and has the fewest serialization bytes
Favicon
Protobuf vs Avro for Kafka, what to choose?

Featured ones: