Set Up Your Real Time Chat App On Amazon EC2 With Docker and FeathersJS
By: Reda Boumahdi
Tags: Feathersjs, Development, Data Engineering, AWS, Docker

Introduction

Amazon AWS is a service where you can borrow a machine (sometimes for free) to do whatever you want. We are going to learn how to set up a server to run a real time Chat App with FeathersJS. FeathersJS is an open source REST and real time API framework.We will need to set up a database and Feathers. We will launch those 2 parts on your Amazon AWS service. This is a simple tutorial, where we will learn how to run your real time app on your Amazon AWS.

Connect to Amazon Web Service, and create an account, this is maybe the most time consuming task!

Create a free AWS EC2 instance and SSH

Connect to your Amazon AWS profile

Select menu -> products -> compute -> Amazon EC2 -> get started with Amazon EC 2 (It is completely free)

Click on EC2 Dashboard on the left and then launch instance

Select Ubuntu server and choose the Free Tier Eligible T2.micro, then click on Next until you reach the security step

We need 2 network rules, the ssh rule on port 22, source custom and the custom tcp, port range 3000, source anywhere, then launch your instance ! This step is important to reveal the port 3000 to the world on tcp, and the port 22 on ssh.

This step is extremely important to connect to your EC2 machine ! Choose to create a new key pair, name your key pair webApp, and download it, then view instance. Congratulations, you have launched your Amazon AWS instance. The next step is to try to connect to this instance.

This step is for linux / mac users, if you have a windows, please follow this link. Create a directory called webApp, and put the webApp.pem in it (if you are a mac user, you may have downloaded a webApp.pem.txt, rename it webApp.pem), navigate inside this directory using bash, then run :

> chmod 400 webApp.pem

to modify the rights on the key.

> ssh -i “webApp.pem” ubuntu@name_of_your_ubuntu_instance

You can find the right command by connecting to the Amazon AWS website, and click on the connect button. A pop-up will appear with the right ssh command. For me for instance, it is going to be :

> ssh -i "webApp.pem" ubuntu@ec2-54-186-155-245.us-west-2.compute.amazonaws.com

Congratulations, most of the dirty work is done by now ! The aim is to be pretty simple, and help you set up your machine.

Install Docker

Now, we need to install Docker on your ec2 machine. Docker runs “kind of” virtual machine which are called images, but are way lighter than virtual machines. Docker will allow you to run all the bricks of your app with a simple command line ! The good thing about Docker is that it encapsulates all the software you may need. This is precisely what we want to do when we run an app, we wrap the different components of our architecture in containers.

To install docker, you can type on your ec2 terminal the following commands :

> curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

> sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

> sudo apt-get update

> apt-cache policy docker-ce

> sudo apt-get install -y docker-ce

> sudo systemctl status docker

The last command is to make sure that docker is installed.

Install Feathers, Get And Launch The Chat App

To install Feathers, and download the app, we will simply pull a Docker image. That is where the magic resides. There is already a Docker image with Feathers installed and the real time chat app coded. We will use this ready to use image, and deploy it to amazon. You can then quit your shell, and the AWS instance will still be running your app ! Let’s try.

Run the following command :

> sudo docker pull kevbac/feathers-chat

Now we need to create a folder and bind it to the docker container. For instance, navigate to your webapp folder and run :

> sudo docker run -itd -p 3000:8080 -v ~/webapp:/home --name feathersChatApp kevbac/feathers-chat bash -c "npm start"

You can notice that the folder is empty, but the app is running! You can copy the content of the docker container to your host machine with this command:

> sudo docker cp feathersChatApp:/usr/src/app .

We now run in a detached mode “npm start”, you can use “yarn start” as well. This means that in the background, we have a process keeping our chat app alive! We can now access to our EC2 Amazon machine through HTTP. Give it a try, copy the public DNS to your browser and add the port :3000 in the end. For me it looks like this :

http://ec2-54-187-133-189.us-west-2.compute.amazonaws.com:3000/

You should have the same picture as this one. Now you can chat with your friends or build a code on top of this “boilerplate”, manage or change the database, etc…

You can signup, login, and test your real time chat app by creating two accounts and exchange messages.

Now Close Your Terminal And Enjoy Your App!

Next steps for the best worflow in dev mode

Create the same project in your local machine. Push this project to github. Develop locally, and deploy by asking your ec2 machine to pull your github repository. Write a Dockerfile and a Makefile to manage your builds / deployments in one command line !

Enjoy agility with an environment iso-staging where you can deploy in one command line.

Conclusion

Today you learned to deploy a real time application to Amazon. Real time is easy to set up with FeathersJS. Docker can help you encapsulate all FeathersJS environment and push it to Amazon. Docker can even help you launch your application on Amazon and restart it when it crashes, but it is not a production ready environment. You can now start coding and enriching your real time application and deploy it to the cloud.

In the next articles, we will deploy deep learning / big data applications. Don’t hesitate to follow me to get notified when the next article comes out!

More links