Unlock the Power of Distributed Applications with Dapr

What is Dapr?

Dapr (Distributed Application Runtime) is an open-source project that simplifies building microservice applications for developers. This event-driven, portable runtime allows you to focus on writing code rather than worrying about the underlying infrastructure.

Building Blocks of Dapr

Dapr consists of several building blocks, accessed through standard HTTP or gRPC APIs, which can be called from various programming languages. These building blocks include:

  • Service Invocation: Enables method calls, including retries, on remote services.
  • State Management: Allows for the storage, retrieval, and deletion of key/value pairs.
  • Publish and Subscribe Messaging: Enables event-driven architectures for horizontal scaling and failure resilience.
  • Event-Driven Source Bindings: Improves scalability and resilience by receiving and sending events to external resources.
  • Virtual Actors: Simplifies concurrency with method and state encapsulation.
  • Distributed Tracing: Compiles trace events, metrics, and performance statistics between Dapr instances.
  • Secrets Management: Provides easy access to secrets without knowing the intricacies of the specific secret store.

Building a Chat App with Dapr

In this example, we’ll explore how to create a chat app using some of Dapr’s building blocks. Our chat app will be built using Node.js and React.

Prerequisites

Before we begin, ensure you have:

  • Node installed on your machine
  • Yarn or npm installed on your machine
  • Docker installed on your machine

Setting Up Dapr

First, install the Dapr CLI on your local machine using the specific install script for your OS. Then, set up Dapr on your machine with the following command:

dapr init

Creating the Node Subscriber

Create a folder structure for our server and add a description of your project to the README.md file.

Create a simple express application that exposes a few routes and handlers and initializes a WebSocket server instance to send messages to connected clients.

// app.js
const express = require('express');
const app = express();
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

app.use(express.static('public'));

app.get('/', (req, res) => {
  res.send('Hello World!');
});

wss.on('connection', (ws) => {
  console.log('Client connected');

  ws.on('message', (message) => {
    console.log(`Received message: ${message}`);
  });

  ws.send('Hello client!');
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

Running the Node Subscriber with Dapr

Run the node-subscriber app with Dapr using the following command:

dapr run --app-id node-subscriber --app-port 9000 --port 3500 node app.js

Setting Up the Chat App Client

Create a folder structure for our client and add a short description of the client to the README.md file. Then, generate the package.json file using the following command:

npm init -y

Installing Dependencies and Setting Up the Frontend Server

Install dependencies and set up the frontend server that invokes the message method through Dapr.

Building the Client-Side Application

Create the main component of our frontend app, which will handle receiving messages from the node-subscriber server and displaying them to the user.

With Dapr, it’s easy to write resilient, scalable microservices. Explore more examples and information in the Dapr GitHub repo.

Leave a Reply

Your email address will not be published. Required fields are marked *