Build a Real-Time Chat in 30 Minutes with WebSockets

PrimeTek Academy
PrimeTek Academy

Wed, 18 Feb 2026

codecamp
Build a Real-Time Chat in 30 Minutes with WebSockets

From zero to live chat in half an hour

REST APIs poll for new data. WebSockets push it the instant it happens. Here's how to build a working chat with Node.js and Socket.io.

Step 1 — Server (10 lines)

const io = require("socket.io")(3000, { cors: { origin: "*" } });
io.on("connection", socket => {
  socket.on("message", msg => io.emit("message", msg));
});

Step 2 — Client

const socket = io("http://localhost:3000");
socket.on("message", msg => appendMessage(msg));
form.addEventListener("submit", () => socket.emit("message", input.value));

Step 3 — Add rooms

Use socket.join(room) and io.to(room).emit() to create separate channels. Add authentication middleware with io.use().

This foundation scales to notifications, live dashboards, multiplayer games, and collaborative editing. WebSockets are the gateway to real-time web development.

Share this article

Comments

0
?
0 / 2000
Loading comments…