ws
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The "ws" is a simple-to-use, supremely fast, and robustly tested WebSocket client and server implementation for Node.js. With "ws", you can effortlessly establish real-time, two-way channels of communication between the server and the client thereby making it a top choice for building real-time applications.
To start using the "ws" library, first install it using npm:
npm install ws
Then instantiate either the WebSocket client or the WebSocket server:
import WebSocket from 'ws';
const ws = new WebSocket('ws://www.host.com/path');
ws.on('open', function open() {
ws.send('something'); // send a message
});
ws.on('message', function message(data) {
console.log('received: %s', data); // print all received messages
});
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function message(data) {
console.log('received: %s', data); // print all received messages
});
ws.send('something'); // send a message
});
Please note, 'ws' does not work in a browser environment. To make the same code work seamlessly between Node.js and the browser, consider using a wrapper like isomorphic-ws.
The extensive documentation for the "ws" library can be found in the ws.md file in the docs folder of the project repository. The API Docs section provides a detailed explanation of all classes and utility functions provided by the "ws" library. The documentation is designed on the lines of Node.js documentation to make it easily comprehensible for developers familiar with Node.js.