# Building a Scalable Chat App

> Real-time + Horizontal Scalability

## Introduction

When I first started building this chat app, there was one requirement I had in mind: it has to be real-time, scalable, and easy to scale up. Sounds easy—until you introduce multiple servers to the mix.

In this article, I'm going to walk you through how I built a horizontally scalable chat app using:

* Socket.io for real-time messaging
    
* Express.js as the backend framework
    
* Redis Pub/Sub for synchronizing messages between servers
    

This is a simple, yet strong—and horizontally scalable for production traffic capable—system.

## Architecture Overview

Here’s a quick look at the architecture I implemented:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1747532042485/d4ba6ee1-18ce-4146-b0ed-72fa3326a442.png align="center")

* Users 1–3 are connected to App Server A
    
* User 4 is connected to App Server B
    
* Both servers are connected to Redis
    
* Redis handles message broadcasting between servers
    

With this setup, the system is horizontally scalable—add app servers whenever needed.

## Why Redis Pub/Sub?

As your application grows, you can no longer rely on a single server. However, if there are lots of servers, broadcasting a chat message to everyone is tricky.

Here's why:

* Suppose User 1 is on Server A, and User 4 is on Server B. A message from one server will never reach the other user—unless servers talk to one another.
    

That's when Redis Pub/Sub comes into play:

* Each app server subscribes to a Redis channel (e.g. chat)
    
* When a message is posted, it's published to the channel
    
* All servers receive it and emit to their connected clients
    

> So Redis is essentially a router of messages between servers.

## Test Scenario

Here’s what I tested:

* User 1 sends a message → hits Server A → published to Redis
    
* Server B receives it via Redis → emits to User 4
    
* Everyone sees the message in real-time
    

> So even though users may be on different app servers, the chat is synchronized.

## Why This Architecture Works

* Stateless app servers – easy to scale horizontally
    
* Redis handles cross-server communication
    
* No message loss or delay
    
* Production-ready
    

## Try It Out

* **Live Demo**: [errasor.io](https://app.eraser.io/workspace/LU14XzUDu6CVz3OMoxjG?origin=share&elements=ciJZ2_jjHUNGqLR7eJfvxg)
    
* **Source Code**: [GitHub Repo](https://github.com/kawaljain/scalable_chat_app.git)
    

## Final Thoughts

Redis Pub/Sub + Socket.IO is an excellent pairing to build highly scalable real-time systems. If you're working on anything from chat through multiplayer games or real-time dashboards—this pattern is worth learning.

I hope you enjoyed this..! Feel free to share your thoughts, questions, or ideas for future topics in the comments.

> *Thanks for reading*
> 
> ***Happy coding!***
> 
> *If you enjoyed the article and would like to show your support, be sure to:*
> 
> *Follow me On* [***Medium***](https://kawaljain.medium.com/)
> 
> Follow me On [**Hashnode**](https://blog.kawaljain.com/)
> 
> *Checkout more* [***Portfolio***](http://kawaljain.com/)
