Realtime Features with WebSockets
Push live updates such as notifications, chat and status changes using WebSockets, Server-Sent Events or a managed service.
Advanced · about 5 hours · difficulty 4/5
Learn first: Sessions, Cookies and JWTs, Node.js Runtime Essentials
Some features need the server to push data: "your application was shortlisted", a live interview chat, or a contest leaderboard.
Options
- Polling: the client asks every few seconds. Simple and often good enough.
- Server-Sent Events (SSE): one-way server-to-client stream over HTTP. Great for notifications and progress updates.
- WebSockets: full two-way connection. Needed for chat, collaborative editing and games. Socket.IO adds rooms, reconnection and fallbacks.
- Managed services (Pusher, Ably, Supabase Realtime) work well with serverless hosting where you cannot hold connections.
Design concerns
- Authentication on connect, and authorization for each room or channel.
- Scaling: with multiple server instances, use a pub/sub layer like Redis so a message reaches users connected to any instance.
- Reconnection and missed messages: store events in the database and let clients catch up.
Pitfalls
- Using WebSockets when polling every 30 seconds would do.
- Trying to hold WebSocket connections inside serverless functions.
- Broadcasting private data to a room without checking membership.
You are ready to move on when your portal shows live application status updates to students, authenticated per user, and still works after a page refresh or reconnect.
Key concepts
- Polling vs SSE vs WebSockets
- Socket.IO rooms and events
- Authenticating connections
- Scaling with Redis pub/sub
- Reconnection and catch-up
- Managed realtime services
Mini project
Build a live notifications feature: when a recruiter changes an application status, the student sees a toast instantly via SSE or Socket.IO, with missed notifications loaded from the database on reconnect.
Check yourself
You scale your WebSocket server to two instances and some users stop receiving messages. Why, and what fixes it?
Free resources
- The WebSocket API (MDN)
- Socket.IO Documentation (Socket.IO)
- Using server-sent events (MDN)