You send a chat message and wait. Nothing happens. You refresh the page, and there it is, three messages late. π
That's what happens when a "live" feature is built on plain HTTP alone. The browser has to keep asking, "Anything new yet?" That's slow, wasteful, and a little embarrassing.
So how do apps like chats, live dashboards, and multiplayer games update instantly? Most of them use WebSockets . In this post, you'll learn what WebSockets are and build the same real-time chat server twice, once with Express and once with NestJS , saving messages in MongoDB . π
What Is WebSockets?
WebSockets is a way for a browser and a server to keep one connection open and talk to each other at any time.
Think of regular HTTP like sending letters. You write a letter (request), the server writes back (response), and the conversation ends. If you want more, you send another letter.
A WebSocket is more like a phone call .β¦