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:
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 right 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:
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.js
. index.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.js
file 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
Woohoo, it works! It returned { result: 6 }
object and that's what we expected!
Conclusion
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/.
I am writing articles like this one on http://ivanjov.com/.
$9.99 Getting started with microservices [From zero to production] Flash Sale 🌟
https://www.udemy.com/getting-started-with-microservices-from-zero-to-production/?couponCode=NEW2018DISCOUNT
Great introduction
Thanks!
hey! i create a microservices framework on nodejs https://github.com/Devisjs. Try it and tell me what do you think? ;) Thanks!