Terminal Chat Application using Serverless Redis

By Rafael Nunes

Rafael Nunes

During coding or working on terminal many Developers/DevOps don’t want to switch Slack and don’t want to lost their focus. I was thinking about this problem and decided to implement a simple solution that can be helpful for small teams that can make a chat using terminal.

So in this blog post we will implement a Terminal Chat application using Serverless Redis and Bless.js. This application will be as simple as possible but according to feedbacks there might be improvements and new features. Please feel free to give a feedback to us!

                       npm i -g taalk
Image for post
Taalk using Serverless Redis

Prerequisites

  • Basic javascript/nodejs knowledge
  • Basic Redis knowledge

Let’s take a quick look for our design:

Image for post

All communication is handled by using Redis pub/sub channels. Every user will subscribe the same channel for incoming messages and all users will publish the messages to same channel.

We will have only 2 file for whole application and we will use Blessed.js and ioredis libraries.

Let’s start to implement chat application. In order to keep simplicity we will only get single argument which will be Redis url. You can get url from the Lambda Store Console with Connect button. Please select ioredis in the menu and you can get the url.

              taalk redis://:$your_redis_url

Note: If TLS is enabled your redis url should start with “rediss

In our index.js, we will check Redis Connection and ask to user for his name. This name will be used in Chat Room

Read the Redis URL from arguments and make initial connection

In above code snippet, we are reading Redis url from the arguments and trying to connect Redis. If connection fails then we immediately exit the program and put example usage command.

Let’s assume that we have connected to Redis. Now we can ask for the name. We will use blessed library for this purpose.

Image for post

Now we have 2 clients for pub/sub and we get the name from the user. So Lets make another screen for ChatRoom and write simple logic for incoming/outgoing messages.

First draw the screen:

Image for post

Now lets write a simple logic for incoming/outgoing messages.

That’s all. We wrote a simple chat application by creating only two node.js file. We drawed the screens and used the redis pub/sub for communication.

Conclusion

https://github.com/Lambda-Store/taalk