DEV Community

Leandro Lima
Leandro Lima

Posted on

Building a Microservice Architecture with Node.js, TypeScript, and gRPC

Building a Microservice Architecture with Node.js, TypeScript, and gRPC

Modern web applications tend to contain a large number of interconnected components that must communicate reliably. Leveraging microservices architecture presented by Node.js, TypeScript, and gRPC can be a great way to build this kind of reliable architecture. In this blog post we will explore building a microservice architecture with these three technologies and showcase how to get started.

What is a Microservice Architecture?

A microservice architecture is an architectural style for developing large-scale applications. It is composed of single-function services that can be independently managed and deployed. This approach has gained tremendous popularity among developers innovating on the web due to its scalability and flexibility.

Why Node.js, TypeScript and gRPC

Node.js is a popular web development framework used for creating web applications and APIs. It is highly performant and enables developers to quickly and easily build sophisticated web applications.

TypeScript is a statically typed language developed by Microsoft. It is a superset of JavaScript that adds features such as type checking and class-based object-oriented programming.

gRPC is a modern, open-source, high performance remote procedure call protocol developed by Google. It is used to make efficient, low-latency remote calls to services over the network.

Building the Architecture

We can now get started on building our architecture. The first step is to create a new directory for our project, initialize the package.json and install the necessary dependencies. In our case, this includes Node.js, TypeScript, and gRPC.

mkdir microservices
cd microservices
npm init -y
npm install --save grpc @types/grpc
Enter fullscreen mode Exit fullscreen mode

Next, let’s install the TypeScript compiler so that we can compile our TypeScript code into JavaScript.

npm install --save typescript
Enter fullscreen mode Exit fullscreen mode

Once we have the dependencies installed, we can write our TypeScript code. Here is a simple example of a gRPC service that returns a list of users.

import { Service, ServerUnaryCall, ServerWritableStream } from 'grpc';

export interface IUserService extends Service {
   listUsers: ServerUnaryCall<GetUserRequest, GetUserResponse>,
}

export class UserService implements IUserService {
  public listUsers = (call: ServerUnaryCall<GetUserRequest, GetUserResponse>): void => {
    const users = [
      { id: 1, name: 'Jane Doe' },
      { id: 2, name: 'John Doe' },
    ];

    call.send(users);
  }
}
Enter fullscreen mode Exit fullscreen mode

Now that we have this service written, we can generate the gRPC service definition. This is done using the protoc command:

protoc -I=api --js_out=import_style=commonjs,binary:. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_plugin` api/user.proto
Enter fullscreen mode Exit fullscreen mode

This will generate two files, user_grpc_pb.js and user_pb.js, which contain the definitions of our gRPC service. We can now use this in our application.

The next step is to create the server. This is done using the grpc.Server class. The server is responsible for setting up the gRPC calls and managing the incoming and outgoing requests and responses.

import * as grpc from 'grpc';
import { UserService, IUserService } from './services/user';

const server = new grpc.Server();

server.addService(UserService as grpc.ServiceDefinition<IUserService>);

server.bind('localhost:50051', grpc.ServerCredentials.createInsecure());
server.start();
Enter fullscreen mode Exit fullscreen mode

Finally, we can create the client. This is done using the grpc.Client class and we can use it to make remote calls to our gRPC server.

import * as grpc from 'grpc';
import { UserService } from './services/user';

const client = new UserService(
  'localhost:50051',
  grpc.credentials.createInsecure(),
);

client.listUsers((err, users) => {
  if (err) {
    console.error(err);
  } else {
    console.log(users);
  }
});
Enter fullscreen mode Exit fullscreen mode

Summary

We have now explored building a microservices architecture with Node.js, TypeScript, and gRPC. We were able to create a service, generate the gRPC service definition, create a server, and create a client. This architecture is highly scalable and resilient and provides a robust and performant solution for organizations looking to modernize their applications.

Top comments (0)