Server setup, emitting events, client connections, namespaces.
Raw WebSockets are powerful but minimal — you get a message channel and that's it. Socket.io adds rooms, namespaces, automatic reconnection, event names, and fallback to HTTP long-polling when WebSockets aren't available. For most real-time apps, Socket.io is the right choice.
ws when you need the lowest latency and smallest payload (gaming, IoT). Use Socket.io for app-level features: chat, notifications, live updates.const express = require('express');
const { createServer } = require('http');
const { Server } = require('socket.io');
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: { origin: '*' } // Configure properly in production
});
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
// Listen for a 'message' event from the client
socket.on('message', (data) => {
console.log('Message from', socket.id, ':', data);
// Emit back to everyone including sender
io.emit('message', { from: socket.id, text: data });
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
});
});
httpServer.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Socket.io uses named events rather than raw messages. This makes your code much easier to read and maintain. There are four main ways to emit:
// To the sender only
socket.emit('event', data);
// To everyone except the sender
socket.broadcast.emit('event', data);
// To everyone including sender
io.emit('event', data);
// To a specific room
io.to('room-name').emit('event', data);
Namespaces let you create separate communication channels on the same server. Each namespace has its own event handlers and rooms — useful for separating admin traffic from user traffic or building multi-tenant apps.
// Server: create a namespace
const adminNS = io.of('/admin');
adminNS.on('connection', (socket) => {
socket.emit('welcome', 'You are in the admin namespace');
});
// Client: connect to a namespace
const adminSocket = io('/admin');
adminSocket.on('welcome', (msg) => console.log(msg));
npm install express socket.ioincrement, the server increments and broadcasts the new count.reset event that only admin clients (use a query param to identify) can send.socket.on('eventName')) make code readable and maintainable.