Quick Intro to Node.JS Microservices: Seneca.JS

What is microservices architecture?

These days, everyone is talking about microservices, but what is it really? Simply said, microservices architecture is when you separate your application into smaller applications (we will call them services) that work together.

I have found one awesome image that represents the difference between monolith and microservices apps:

I have found one awesome image that represent difference between monolith and microservice apps

Picture above explains it. On the left side we see one app that serves everything. It’s hosted on one server and it’s usually hard to scale. On the left side, we see the microservice architecture. This app has different service for each functionality. For example: one is for user management (registration, user profiles…), second one for emails (sends emails, customises templates..), third one for some other functionality.. These services communicate using API (usually send JSON messages) and they can be on same server. But the good thing is that they can be spread through different servers or Docker containers.

How can I use NodeJS to make microservices architecture?

So, you want to use NodeJS to create microservices architecture? That’s very simple and awesome!

In my career, I’ve used many frameworks and libraries for creating microservices architecture, even created custom libraries (don’t do it!) — until I found SenecaJS. To explain what Seneca is, I will quote the official website:

Seneca is a microservices toolkit for Node.js. It helps you write clean, organized code that you can scale and deploy at any time.

Simple! Basically, it helps you exchange JSON messages between your services and have good looking and readable codebase.

Seneca uses actions. There are action definitions and action calls. We can store our action definitions inside our services and call them from any service. For understanding how Seneca works, you need to think about modular pattern and avoid the desire to put everything inside one file.

I am going to show you how it works!

Let’s play!

For this tutorial, we are going to build a simple app. Yay!

First, let’s create simple a NodeJS app:

npm init

It will go through installation and install this:enter image description here

Then, we will install Seneca:

npm install seneca --save

It will install all modules we need and we can just require Seneca and use it.

Before we start, let me explain a couple more things to you. There aren’t any conventions about what we should put inside our JSON objects, but I have found out that lot of people use the same style. I am using this one {role:'namespace', cmd:'action'} and I recommend you to stick to this one. Creating a new style can lead to problems if you work in a team. Role is the name of group of functions and cmd is the name of the action. We use this JSON to identify which function we are going to use.

I will create two files, index.js and process.jsindex.js will send a request to process.js with some numbers, sum it up, then return result. The result will be written in console from index.js file. Sounds good? Let’s start!


Here is the code from process.js:

module.exports = function( options ) {
  var seneca = this;

  seneca.add( { role:'process', cmd:'sum' }, sum );

  function sum ( args, done ) {
    var numbers = args.numbers;

    var result = numbers.reduce( function( a, b ) { 
      return a + b; 
    }, 0);

    done( null, { result: result } );

Here, we define the function sum and add to Seneca using the seneca.add function. The identifier is { role:'process', cmd:'sum' }sum calls the done function and sends the result object. The Seneca function returns objects by default, but it can be customized to return string, or number, too.

Finally, here is the code from index.js:

var seneca = require('seneca')();

seneca.use( './process.js' );

seneca.act( { role: 'process', cmd: 'sum', numbers: [ 1, 2, 3] }, function ( err, result ) {
  console.log( result );
} )

As you can see, we use seneca.use to tell Seneca that we are going to use process.jsfile and that we defined our function there. In the next lines, we use seneca.act to call the function from process.js. We are sending the JSON object with role and cmd, along with the arguments numbers. The object result is returned and it should contain our result. Let’s test it:

node index.js

Function result

Woohoo, it works! It returned { result: 6 } object and that’s what we expected!


Seneca is awesome, it has big a potential and you can create more complex apps with it. You can run multiple node processes and use the same services in process and a lot of other cool stuff. I will write more about this topic. Stay tuned!

I hope you liked this introduction and tutorial to Seneca.js! If you want to learn more about it, you can check out SenecaJS website and their API page: http://senecajs.org/api/.

Source: https://www.codementor.io/ivan.jovanovic/tutorials/introduction-to-nodejs-microservices-senecajs-du1088h3k



Node.js debugging with Chrome DevTools

Node.js debugging with Chrome DevTools (in parallel with browser JavaScript)

Serg Hospodarets BlogSeptember 29, 2016

Recently Paul Irish described how you can debug Node.js applications with Chrome DevTools.

Since that time Chrome DevTools have evolved and the step, where you had to open the separate page with a specific URL to debug the Node.js code, was removed.

It means, today you can debug your browser JavaScript files and Node.js ones in the same DevTools window in parallel, which makes the perfect sense.

Let’s take a look how it works.

What you need

1) Node.js 6.3+

You can install it directly from the Node.js site or switch to it using nvm (Node Version Manager)

Better to you use 6.6+ as Paul Irish mentioned in the comment, “in 6.4 there are a few flaky bugs”.

2) Chrome 55+

For that, you can download Chrome Canary

If you are using Atom, try this https://atom.io/packages/node-debugger

Enable a new way of Node.js debugging in Chrome

Currently the parallel debugging of the browser JavaScript and Node.js code is a new feature and now is qualified as an experiment.

To enable it, you have to do the following:

  • Open the chrome://flags/#enable-devtools-experiments URL
  • Enable the Developer Tools experiments flag
  • Relaunch Chrome
  • Open DevTools Setting -> Experiments tab (it started being visible after the reload)
  • Press "SHIFT" 6 times (enjoy it ¯ \ _ (ツ) _ / ¯) to show the hidden experiments
  • Check the "Node debugging" checkbox
  • Open/close DevTools


To start debugging, just open your page in Chrome and DevTools, as usually.

Start Node.js app

Start the Node.js in the debug mode. For that add the --inspect argument. E.g.:

node --inspect node.js

If you do this, you’ll see the output from Node.js, that it started in debug mode and an option to inspect the process opening a separate URL in Chrome:


Debug in DevTools

But we want to debug it in parallel with our browser JavaScript, so switch back to your Chrome.

If you have any console.log or similar output in your Node.js application (outlined with blue on the previous image) , you can notice, that it already appeared in Chrome DevTools console:

After that, you can e.g. put breakpoints in both browser and Node JavaScript files and debug them.

I prepared a demo:

It contains a usual static server using Node.js (node.js file) and a page which just makes fetch requests to the pointed URL (the code is in the browser.js file).

You can try it to see how easily you can debug Node.js in Chrome. Just download the demo code from Github and run node --inspect node.js in its folder.

After that open http://localhost:8033/ in Chrome and so you can debug both browser.js and node.jsthe same time:


As you see, you can put breakpoints in the Node.js code and output from Node.js apps goes to the DevTools console.

But there are many other abilities:

  • Live Edit: you can not only debug, but also change the files content
  • Profile JavaScript
  • Take snapshots etc.


If you use Node.js for your project, now you can debug and make changes for all your JavaScript from one place- Chrome DevTools.

You also can use all the power of Chrome DevTools applying it to Node.js code.

Source: https://blog.hospodarets.com/nodejs-debugging-in-chrome-devtools

Other sources for javascript debugging:

Debugging JavaScript:    https://developer.chrome.com/devtools/docs/javascript-debugging

NodeJS inbuilt debugger: https://nodejs.org/api/debugger.html

Node-inspector: https://github.com/node-inspector/node-inspector

Atom-node-debugger: https://atom.io/packages/node-debugger