Building a Scalable Chat App

Kawal JainKawal Jain
3 min read

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:

  • 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

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

Follow me On Hashnode

Checkout more Portfolio

0
Subscribe to my newsletter

Read articles from Kawal Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kawal Jain
Kawal Jain

🧑‍💻 Full Stack Developer | JavaScript & GenAI Enthusiast 🚀 7+ years building scalable web apps with React, Node.js & PHP 💡 Exploring GenAI, Microservices, and Clean Code Principles 📚 Blogging my dev journey, lessons learned, and side experiments ♟️ Chess lover | Vegetarian foodie | Life-long learner