Socket.IO
Realtime bidirectional event framework with automatic reconnection, rooms, acknowledgements, and HTTP long-polling fallback.
Official Documentation ↗4.8.4. Check the official docs for the latest API surface.Installation
npm install socket.iopnpm add socket.ioyarn add socket.ioMethods
No methods match this filter.
Socket.IO provides an event-based channel between browsers and servers. It keeps a persistent connection, falls back to HTTP long polling when WebSocket is blocked, reconnects automatically, and buffers outgoing events while a client is briefly offline.
Overview
The server attaches to a Node HTTP server and emits events to connected sockets. Events are named strings with arbitrary payloads; acknowledgements let a client confirm delivery. Rooms group sockets for broadcasts, and handshake middleware authenticates connections before any event handler runs. A compatible client is required because the wire protocol is not plain WebSocket.
Installation
Install socket.io on the server and socket.io-client in the browser or Node client. Attach the server to the same HTTP server that serves your app so cookies and CORS rules stay consistent. In production, terminate TLS at the proxy, enable sticky sessions, and add a Redis adapter when more than one instance handles traffic.
Core API walkthrough
new Server(httpServer, options) wires the engine and CORS policy. socket.emit sends an event to one client, while socket.on receives one, optionally with an acknowledgement callback. socket.join adds a socket to a room and io.to(room).emit broadcasts to it. io.use authenticates the handshake, and the client io(url, options) opens the connection with auth data.
Pitfalls
Events are fire-and-forget unless an acknowledgement is used, so delivery is not guaranteed across disconnects. Room membership is in-memory per process, which breaks with multiple instances unless an adapter is added. Auth tokens sent in query strings leak into logs; use the auth option. Also remove listeners on unmount to avoid duplicates.
Verification
Open two browser tabs and confirm a broadcast reaches both while a direct emit reaches one. Reject an invalid token and check the client receives connect_error. Stop and restart the server to observe reconnection, and inspect the handshake request in DevTools to confirm the chosen transport and auth payload.
FAQ
- Can a plain WebSocket client connect to Socket.IO?
- No. Socket.IO adds its own protocol, heartbeats, and reconnection on top of WebSocket, so clients must use the socket.io-client library. Native WebSocket servers require a different implementation.
- Why do event handlers fire twice after reconnecting?
- Each new connection creates a new socket instance. Client listeners registered outside the connect handler persist, while server-side listeners are re-registered per connection, so duplicate handlers usually come from registering on every render.
- How do I scale Socket.IO across multiple servers?
- Use an adapter such as @socket.io/redis-adapter so broadcasts reach sockets connected to other instances. Sticky sessions are still required when long polling is enabled behind a load balancer.
Report a problem with this API docOpens a pre-filled issue on GitHub.