DEV Community

Ko
Ko

Posted on

1

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 git@github.com: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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more