Socket.IO Template
The Socket.IO Template gives you a ready-to-run real-time backend built on Express and Socket.IO.
It’s ideal for chat apps, live dashboards, notifications, or multiplayer systems where low-latency updates matter.
Overview
This template extends the Basic Express setup to include real-time bidirectional communication.
It automatically configures:
- A shared HTTP + Socket.IO server
- Centralized socket initialization (
src/config/socket.js) - A clean folder structure for organizing events and logic
- Support for both REST routes and Socket events in one project
[!TIP] See the Folder Structure Guide for detailed project layouts.
Example Socket Initialization
// src/config/socket.js
import { Server } from "socket.io"
export const initSocket = (server) => {
const io = new Server(server, {
cors: { origin: "*" }
})
io.on("connection", (socket) => {
console.log(`User connected: ${socket.id}`)
socket.on("disconnect", () => console.log("User disconnected"))
})
return io
}And in server.js:
import http from "http"
import app from "./src/app.js"
import { initSocket } from "./src/config/socket.js"
const server = http.createServer(app)
initSocket(server)
server.listen(5000, () => console.log("Server running on port 5000"))Features
- Real-time event-driven architecture
- Shared HTTP and WebSocket layers
- Event handlers organized by module
- Compatible with REST routes in the same project
- CORS and connection logging enabled by default
Related Topics
Explore:
- Events — how to register and manage real-time listeners
When to Use
Use this template when your project requires:
- Real-time updates (chat, dashboards, notifications)
- Persistent user sessions via websockets
- Live collaboration features
- Seamless REST + Socket integration in one backend
This setup handles both HTTP and WebSocket traffic efficiently — ready to scale from prototype to production.