Build a simple chat app with node.js and Berlioz

Jul 24 · 4 min read

In this post, you will learn how to create a simple chat application using Berlioz and node.js!

Some of the problem with working with microservices, it’s the communication between them, you need to spend time assigning IP address and then internally configuring the firewall so they can both communicate.

In this case it’s a chat example made in node.js where two microservices communicate each other, one called web, and other called App, you can see it in this graph

structure

Getting Started

We will create a server script and a client script (that will connect to the server). We need to first set up the server configuration.

Starting off with a few includes:

Creation of Berlioz file

Berlioz is a tool which configures cloud infrastructure and deploys services

Berlioz configuration is defined as a part of the application code base using YAML definitions. Should be defined in files named “Berliozfile” or “any-name.Berliozfile”. The tool will scan the project directory structure and use those configurations for deployment:

In case we are defining a database, we should put in kind “database”

If we also need to run a script we should define it

In case we are defining a microservice, we should put in kind “service”, if this microservices use a database or another service to communicate, we should add it also in the configuration

Berlioz definitions above produce application deployment diagram below

Command: berlioz output-diagram

Using Berlioz in the code

We need a Berlioz sdk

And also assign it which provider and which handle we are gonna use

We need also a client, in the case of MySQL, it needs some extra configuration like telling the user,password and the database

To use it, in case it’s MySQL database we just need to run

This returns a promise within content resolved, it’s the data from the query, in case of an insert it’s the same as above, but no data will be in the resolve content

To use it, in case of Google Cloud Storage, first we need the name/id of the file, most be unique across files, we separate for reading and writing

For reading we use createReadStream().

For writing we use createWriteStream()

Running locally

Now we will need some tool like: Docker, Node.js, graphviz, Google Cloud project and it’s credential with owner permissions, and also berlioz CLI:

Setting credentials :

Wee need first to assign the credentials from a Google Cloud project, and give it owner permissions, then you need to set the credentials to berlioz

Then run the application with

And wait until berlioz configures everything, once it’s completed, we just go to http://127.0.0.1:40000 and test the app

Once you have finished testing the app, it’s import to stop it, so to stop it:

Now moving to the cloud

First, you will need to login with you account

Then you need to set the credentials for the cloud

Create a deployment

And deploy it

Now go to https://console.cloud.google.com/kubernetes and inside will see the IPs for the app, go for the one that says “web”, and that’s it

Now to stop it just run

Why Berlioz ?

  • Easy communication between microservices, it’s quite easy to work with microservices, also you don’t need to get headache by IP configurations, that tend to be tedious
  • Easy running in local machine.
  • Easy transition from local machine to the cloud.

Hope this tutorial was helpful!

You can see the sources and try to run the project on your own here https://github.com/johnbalvin/berliozchat

In case you want to try it, go to : https://docs.berlioz.cloud

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade