Creating a Dapr Project with Node.JS and the Dapr-JS SDK

Dapr is simply amazing, allowing you to interact with libraries that otherwise take a couple of hours to implement or even days depending, on your level of comfort with them. Next to that, when you would utilize libraries, you also need to take care of correct authentication, error handling, and more.

Introduction

Dapr offers all of the above out of the box, removing the need to write code that interacts explicitly with each library. Instead, add a components file, and we are ready to go.

In my previous blog articles, I covered:

One disadvantage so far has been that I am primarily working in full-stack development, utilizing Javascript as my primary to-go-to language. On this side, Dapr has lacked so far its current SDK implementation only offers gRPC out of the box with documentation that could use some work.

This is of course understandable seeing the issue they are trying to solve is a pretty complicated one and creating SDKs for each one of them is not a trivial task. Which is why I would love to help!

Introducing the Dapr-JS SDK

The past couple of months I have been working on a new Dapr JS SDK that is easy to use for Typescript developers! After a lot of work on this and utilizing it myself in production for one of my projects and developing for the other, I am now happy to announce that I am now confident enough that the public can use it!

I am proud to announce the first release of the new Dapr JS SDK!

Note: at the current time of writing, this is still hosted on my Repository, but I am working with Dapr team to move this towards the official repository.

Example - Invoking Across Microservices

A major point I want to focus on through this API is the easiness of use. Removing the need of going to the Dapr API reference documentation all the time, such that you as a developer, only have to focus on utilizing the SDK for your application.

As an example, we can take 2 Microservices my-server and my-client, where my-client wants to invoke a /hello-world endpoint on the my-server application. Through this SDK you can do the following:

Server

Start with dapr run --app-id my-server --app-port 5000 npm run start

import Dapr, { HttpMethod, HttpStatusCode, Req, Res } from "@roadwork/dapr-js-sdk/http";
const client = new Dapr("127.0.0.1", 3500, 4000);
await client.invoker.listen("method", async (req: Req, res: Res) => console.log(req), { method: HttpMethod.POST });

Client

Start with dapr run --app-id my-client --app-port 5000 npm run start

import Dapr, { HttpMethod, HttpStatusCode, Req, Res } from "@roadwork/dapr-js-sdk/http";
const client = new Dapr("127.0.0.1", 3500);
await client.invoker.invoke("my-server", "method", HttpMethod.POST, { hello: "world" });

Demo

Check out the source code here

Future

Of course, Rome was not built in a day, and neither is this SDK. Most of the APIs are covered, but they have to be maintained to stay up-to-date with the upstream Dapr references. Next to that, the current state focuses mainly on the HTTP interaction side of things, while Dapr also offers gRPC support. For me, this is a crucial must-have since it would make interactions way faster, but it should keep the current API definitions that were put in place.

To summarize the focus points:

  • Maintain with upstream API Reference
  • Implement gRPC under @roadwork/dapr-js-sdk/grpc
  • Find additional contributors

If you'd love to help out, feel free to reach out to me on Twitter or Discord (Dapr Server)!