ws
ws is the most widely used WebSocket implementation for Node.js, providing a simple, fast, and thoroughly tested client and server. Unlike Socket.IO which adds …
Installation
npm install ws
yarn add ws
pnpm add ws
Import
import { WebSocketServer } from 'ws';Quick Example
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', (ws) => {
ws.on('message', (data) => {
ws.send(`Echo: ${data}`);
});
});About ws
ws is the most widely used WebSocket implementation for Node.js, providing a simple, fast, and thoroughly tested client and server. Unlike Socket.IO which adds its own protocol layer on top of WebSockets, ws implements the raw WebSocket protocol (RFC 6455) directly, making it compatible with any WebSocket client including browser-native WebSocket objects. The library provides both a WebSocket server that can run standalone or be attached to an existing HTTP/HTTPS server, and a WebSocket client for connecting to WebSocket endpoints. ws supports binary messages, ping/pong for connection health checking, per-message deflate compression, and connection upgrade handling. The server tracks connected clients and supports custom upgrade authentication logic for verifying connections before accepting them. ws is intentionally minimal — it does not include reconnection logic, room management, or message serialization, leaving those concerns to application code or higher-level libraries. This simplicity and protocol compliance make ws the foundation for many Node.js WebSocket libraries and frameworks. It is the recommended choice when you need raw WebSocket performance without additional protocol overhead.
Quick Facts
| Package | ws |
| Category | HTTP |
| Weekly Downloads | 20M+ |
| License | MIT |
| Install | npm install ws |
Related Packages
Socket.IO is a library that enables low-latency, bidirectional, and event-based communication betwee…
Express is the most widely used web application framework for Node.js, providing a minimal and flexi…
Undici is a high-performance HTTP/1.1 client for Node.js, developed by the Node.js team and serving …
http-proxy-middleware is a one-liner Node.js proxy middleware for Express, Connect, and browser-sync…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.