Building Microservices with Seneca.js: A Practical Guide
technologyGeneral ProgrammingJavaScriptdatabaseapibackend

Building Microservices with Seneca.js: A Practical Guide

S
Sylvester Das
1/22/2025
3 min

This article explores the power of Seneca.js, a microservices toolkit for Node.js, to build robust and scalable applications. We'll demonstrate how to create interconnected microservices, using Fastify for our API gateway, and illustrate the benefits of this approach with practical examples.

Introduction

Microservices architecture offers numerous advantages, including independent deployment, improved fault isolation, and better scalability. Seneca.js provides a clean and organized way to implement this architecture in Node.js. This article will guide you through building a simple e-commerce system with two microservices: a product catalog service and an order processing service. We'll use Fastify to create a unified API gateway for clients to interact with these services.

Core Concepts: Seneca.js and Microservices

Seneca.js operates on the concept of actions and patterns. A service defines actions it can perform, described by patterns. Other services or clients can then call these actions using the corresponding patterns. This pattern-matching approach decouples services, allowing them to evolve independently.

Building Our E-commerce System

Prerequisites

  • Node.js and npm installed
  • Basic understanding of JavaScript and Node.js

Step 1: Setting Up the Project

Create a new directory for your project and initialize it with npm:

mkdir seneca-microservices
cd seneca-microservices
npm init -y

Create three subdirectories: api-gateway, product-service, and order-service.

Step 2: Creating the Product Service

Navigate to the product-service directory and install Seneca:

npm install seneca

Create a file named product-service.js:

const Seneca = require('seneca');

const productService = Seneca();

productService.add('role:product,cmd:get', async (msg, reply) => {
  // Simulate database retrieval
  const products = [
    { id: 1, name: 'T-shirt', price: 20 },
    { id: 2, name: 'Jeans', price: 50 },
  ];
  reply(null, products);
});

productService.listen(9001);

This code defines a service that listens on port 9001. It registers an action role:product,cmd:get which returns a list of products.

Step 3: Creating the Order Service

Navigate to the order-service directory, install Seneca, and create order-service.js:

const Seneca = require('seneca');

const orderService = Seneca();

orderService.add('role:order,cmd:create', async (msg, reply) => {
  const order = {
    items: msg.items,
    total: msg.total,
  };
  // Simulate order creation logic
  console.log('Order created:', order);
  reply(null, { order_id: 123 });
});

orderService.listen(9002);

This service listens on port 9002 and defines an action role:order,cmd:create to create orders.

Step 4: Building the API Gateway with Fastify

Navigate to the api-gateway directory and install Fastify and Seneca:

npm install fastify seneca

Create api-gateway.js:

const Fastify = require('fastify');
const Seneca = require('seneca');

const fastify = Fastify({ logger: true });
const productService = Seneca();
const orderService = Seneca();

productService.client({ port: 9001 });
orderService.client({ port: 9002 });

fastify.get('/products', async (request, reply) => {
  const products = await productService.actAsync('role:product,cmd:get');
  reply.send(products);
});

fastify.post('/orders', async (request, reply) => {
  const order = await orderService.actAsync('role:order,cmd:create', request.body);
  reply.send(order);
});

fastify.listen(3000);

The API gateway acts as a single entry point. It uses Seneca's client() method to communicate with the other services.

Testing

Start each service in separate terminals:

node product-service.js
node order-service.js
node api-gateway.js

You can now use tools like curl or Postman to test the API.

Conclusion

Seneca.js provides a powerful and elegant way to build microservices in Node.js. By combining it with Fastify, you can create a robust and scalable API gateway that simplifies communication between your services and clients. This example demonstrates a basic setup, but Seneca's flexibility allows for complex interactions and advanced patterns for building sophisticated microservice architectures. You can expand this example by adding error handling, service discovery, and other features to create a production-ready system.


Follow Minifyn:

Try our URL shortener: minifyn.com

Share this article

Connect with MiniFyn

Join our community for updates and discussions

Building Microservices with Seneca.js: A Practical Guide - MiniFyn - Quick Links & QR Codes for Devs