socket.io, uws, and ws are all npm packages that enable real-time, bidirectional communication between clients and servers using WebSockets or similar protocols. ws is a minimal, standards-compliant WebSocket implementation for Node.js. socket.io builds on top of WebSocket-like transports to provide additional features like automatic reconnection, rooms, and fallbacks for older browsers. uws was an alternative high-performance WebSocket server library, but it has been deprecated and is no longer maintained.
When building real-time web applications — like chat apps, live dashboards, or multiplayer games — choosing the right WebSocket library is critical. The three packages under review (socket.io, uws, and ws) represent different philosophies: high-level convenience, raw performance (now deprecated), and minimal standards compliance. Let’s examine how they differ in practice.
Before diving into features, it’s essential to address maintenance status:
uws is deprecated. The author archived the GitHub repository and unpublished the package from npm. According to the official GitHub repo, the project is no longer maintained, and users are advised to consider alternatives. Do not use uws in new projects.This leaves us with socket.io and ws as viable options — one opinionated and feature-rich, the other lean and standards-focused.
socket.io does not strictly implement the WebSocket protocol. Instead, it uses a custom protocol layered over WebSocket (or HTTP long-polling as a fallback). This allows it to support older browsers and add features like acknowledgments and binary support with metadata.
// socket.io: client and server speak a custom protocol
const io = require('socket.io')(server);
io.on('connection', (socket) => {
socket.emit('welcome', { message: 'Hello!' });
socket.on('chat', (data) => {
// Handle message
});
});
ws implements the RFC 6455 WebSocket standard exactly. There’s no fallback transport — if the client doesn’t support WebSockets, it won’t work. This keeps the protocol lean and interoperable with any standards-compliant client.
// ws: pure WebSocket standard
const WebSocket = require('ws');
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
ws.send(JSON.stringify({ message: 'Hello!' }));
ws.on('message', (data) => {
const msg = JSON.parse(data);
// Handle message
});
});
uws (deprecated) also aimed for RFC 6455 compliance but used a different C++ backend for performance. However, since it’s unmaintained, code examples are omitted, and migration is strongly recommended.
socket.io handles disconnections and reconnections automatically. The client retries with exponential backoff, and the server tracks session state (like rooms) across reconnects using a unique ID.
// socket.io: automatic reconnection (client-side)
const socket = io('http://localhost:3000', {
reconnection: true,
reconnectionAttempts: Infinity
});
ws provides no built-in reconnection logic. You must implement retry strategies, connection health checks, and state recovery yourself.
// ws: manual reconnection
function connect() {
const ws = new WebSocket('ws://localhost:8080');
ws.on('close', () => setTimeout(connect, 1000)); // simple retry
}
connect();
socket.io includes first-class support for grouping connections:
/admin, /chat).// socket.io: rooms and broadcast
io.on('connection', (socket) => {
socket.join('room1');
socket.to('room1').emit('msg', 'To room only');
io.emit('msg', 'To all'); // broadcast to everyone
});
ws has no concept of rooms or namespaces. You must build your own mapping of connections to groups using Set or similar data structures.
// ws: manual room management
const rooms = new Map();
wss.on('connection', (ws) => {
const roomId = 'room1';
if (!rooms.has(roomId)) rooms.set(roomId, new Set());
rooms.get(roomId).add(ws);
ws.on('close', () => {
rooms.get(roomId).delete(ws);
});
});
// Broadcast to room
function broadcast(roomId, message) {
const room = rooms.get(roomId);
if (room) room.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
socket.io supports event-based messaging with optional callbacks for acknowledgment:
// socket.io: acknowledgment
socket.emit('update', data, (response) => {
console.log('Server confirmed:', response);
});
// Server
socket.on('update', (data, callback) => {
// process...
callback({ success: true });
});
ws only sends raw messages. To implement acknowledgments, you’d need to design your own message format with IDs and response tracking.
// ws: manual ack system
const pendingAcks = new Map();
ws.on('message', (data) => {
const msg = JSON.parse(data);
if (msg.type === 'request') {
// send response with same id
ws.send(JSON.stringify({ id: msg.id, type: 'response' }));
} else if (msg.type === 'response') {
const resolve = pendingAcks.get(msg.id);
if (resolve) resolve(msg);
}
});
socket.io works in virtually all browsers, including very old ones, because it falls back to HTTP long-polling when WebSockets aren’t available.
ws requires a WebSocket-capable client. Modern browsers support it, but you lose compatibility with legacy environments unless you add your own fallback layer.
Because socket.io uses a custom protocol, a socket.io client cannot communicate with a ws server, and vice versa. You must use matching client and server libraries.
socket.io-client with socket.io server.WebSocket or ws client with ws server.| Feature | socket.io | ws | uws |
|---|---|---|---|
| Status | ✅ Actively maintained | ✅ Actively maintained | ❌ Deprecated |
| Protocol | Custom (WebSocket + fallbacks) | RFC 6455 WebSocket | RFC 6455 (unmaintained) |
| Reconnection | ✅ Built-in | ❌ Manual | ❌ (Not applicable) |
| Rooms / Broadcast | ✅ Built-in | ❌ Manual | ❌ (Not applicable) |
| Acknowledgments | ✅ Built-in | ❌ Manual | ❌ (Not applicable) |
| Browser Fallbacks | ✅ Long-polling | ❌ None | ❌ None |
| Overhead | Higher (metadata, protocol framing) | Minimal (raw frames) | Low (but unmaintained) |
Use socket.io when you’re building a real-time app quickly and need reliability across networks and browsers. Great for chat apps, collaboration tools, or live notifications where developer velocity matters more than micro-optimizations.
Use ws when you’re building a high-throughput service (like a game server or financial feed) where every byte and millisecond counts, and you’re comfortable managing connection logic yourself.
Never use uws in new code. If you inherit a project using it, plan a migration to ws or socket.io.
If you’re moving from uws to ws, the APIs are somewhat similar. Replace uWS.App().ws() with new WebSocket.Server(), and adjust message handlers to use on('message') instead of message callbacks. Remember to handle backpressure and connection limits explicitly, as ws doesn’t auto-throttle.
In the end, both socket.io and ws are excellent choices — just for different jobs. Pick the one that matches your team’s needs for control versus convenience.
Choose socket.io if you need built-in features like automatic reconnection, rooms/namespaces, message acknowledgment, and cross-browser compatibility with HTTP long-polling fallbacks. It’s ideal for applications requiring rapid development of real-time features without managing low-level connection logic, though it adds protocol overhead compared to raw WebSockets.
Do not use uws in new projects — it has been officially deprecated by its author and is no longer maintained. The package was removed from npm due to licensing and maintenance concerns, and the repository is archived. Existing projects should migrate to alternatives like ws or socket.io.
Choose ws if you need a lightweight, standards-compliant WebSocket implementation with minimal overhead and full control over the connection lifecycle. It’s best suited for performance-sensitive applications where you can manage reconnection logic, message serialization, and browser compatibility yourself, and don’t require Socket.IO’s extra features.
Socket.IO enables real-time bidirectional event-based communication. It consists of:
Some implementations in other languages are also available:
Its main features are:
Connections are established even in the presence of:
For this purpose, it relies on Engine.IO, which first establishes a long-polling connection, then tries to upgrade to better transports that are "tested" on the side, like WebSocket. Please see the Goals section for more information.
Unless instructed otherwise a disconnected client will try to reconnect forever, until the server is available again. Please see the available reconnection options here.
A heartbeat mechanism is implemented at the Engine.IO level, allowing both the server and the client to know when the other one is not responding anymore.
That functionality is achieved with timers set on both the server and the client, with timeout values (the pingInterval and pingTimeout parameters) shared during the connection handshake. Those timers require any subsequent client calls to be directed to the same server, hence the sticky-session requirement when using multiples nodes.
Any serializable data structures can be emitted, including:
Sample code:
io.on('connection', socket => {
socket.emit('request', /* … */); // emit an event to the socket
io.emit('broadcast', /* … */); // emit an event to all connected sockets
socket.on('reply', () => { /* … */ }); // listen to the event
});
Browser support is tested in Sauce Labs:
In order to create separation of concerns within your application (for example per module, or based on permissions), Socket.IO allows you to create several Namespaces, which will act as separate communication channels but will share the same underlying connection.
Within each Namespace, you can define arbitrary channels, called Rooms, that sockets can join and leave. You can then broadcast to any given room, reaching every socket that has joined it.
This is a useful feature to send notifications to a group of users, or to a given user connected on several devices for example.
Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either. Please see the protocol specification here.
// with npm
npm install socket.io
// with yarn
yarn add socket.io
The following example attaches socket.io to a plain Node.JS
HTTP server listening on port 3000.
const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
client.on('event', data => { /* … */ });
client.on('disconnect', () => { /* … */ });
});
server.listen(3000);
const io = require('socket.io')();
io.on('connection', client => { ... });
io.listen(3000);
import { Server } from "socket.io";
const io = new Server(server);
io.listen(3000);
Starting with 3.0, express applications have become request handler
functions that you pass to http or http Server instances. You need
to pass the Server to socket.io, not the express application
function. Also make sure to call .listen on the server, not the app.
const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
Like Express.JS, Koa works by exposing an application as a request
handler function, but only by calling the callback method.
const app = require('koa')();
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
To integrate Socket.io in your Fastify application you just need to
register fastify-socket.io plugin. It will create a decorator
called io.
const app = require('fastify')();
app.register(require('fastify-socket.io'));
app.ready().then(() => {
app.io.on('connection', () => { /* … */ });
})
app.listen(3000);
Please see the documentation here.
The source code of the website can be found here. Contributions are welcome!
Socket.IO is powered by debug.
In order to see all the debug output, run your app with the environment variable
DEBUG including the desired scope.
To see the output from all of Socket.IO's debugging scopes you can use:
DEBUG=socket.io* node myapp
npm test
This runs the gulp task test. By default the test will be run with the source code in lib directory.
Set the environmental variable TEST_VERSION to compat to test the transpiled es5-compat version of the code.
The gulp task test will always transpile the source code into es5 and export to dist first before running the test.
Support us with a monthly donation and help us continue our activities. [Become a backer]
Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]