Projects

Chat App

Build a real-time chat application. Learn message rendering with grouping, typing indicators, user presence, scroll management, XSS prevention, and the EventEmitter pub/sub pattern.

What You Will Build

1

Message Rendering

Group consecutive messages, timestamps, sender avatars

2

Typing Indicator

Show animated dots when another user is typing

3

User Presence

Online/offline status, last seen, user list

4

WebSocket Ready

EventEmitter store pattern — swap localStorage for WS

Message Status Flow

sending
→ sent
→ delivered
→ read
→ failed

Common Pitfalls

Rendering user text with innerHTML — XSS attack: attacker sends <script>steal(document.cookie)</script>
Always escape HTML: replace < > & " before inserting. Use textContent for plain text, or a sanitizer library.
Scrolling to bottom on every render — disrupts users reading older messages
Only auto-scroll if the user was already at the bottom (scrollTop + clientHeight >= scrollHeight - threshold).
Firing typing events on every keystroke — floods server with events
Throttle: send typing:start once, use a debounced timeout to send typing:stop after 2-3 seconds of no input.
No optimistic messages — user clicks Send, nothing appears until server responds
Add message to local state immediately with status:sending. Update to sent/failed when server responds.

Build It Live

Code Editor

<div class="chat">
  <ul id="messages"></ul>
  <form id="form"><input id="text" placeholder="Message" autocomplete="off"><button>Send</button></form>
</div>

Preview

Chat App — step by step

1 / 4
1

WebSocket Connection

A chat app needs a persistent two-way connection. WebSockets open a single TCP connection and keep it alive, letting both client and server push messages instantly without polling.

1 of 4
🔍Use a Message Queue for Scale
A single Node.js process can handle thousands of WebSocket connections, but across multiple server instances you need a pub/sub broker (like Redis Pub/Sub) so a message received by server A is broadcast to clients connected to server B.

🎯 Quick Check

Why do chat applications use WebSockets instead of regular HTTP requests?

Next project

Client-Side Routing