Gathering detailed insights and metrics for websocket
Gathering detailed insights and metrics for websocket
Gathering detailed insights and metrics for websocket
Gathering detailed insights and metrics for websocket
faye-websocket
Standards-compliant WebSocket server and client
websocket-driver
WebSocket protocol handler with pluggable I/O
websocket-extensions
Generic extension manager for WebSocket connections
ws
Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js
A WebSocket Implementation for Node.JS (Draft -08 through the final RFC 6455)
npm install websocket
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (92.82%)
HTML (5.27%)
EJS (1.13%)
CSS (0.52%)
Shell (0.17%)
Makefile (0.09%)
Total Downloads
260,935,757
Last Day
69,060
Last Week
1,083,352
Last Month
4,849,500
Last Year
47,809,528
Apache-2.0 License
3,778 Stars
547 Commits
603 Forks
142 Watchers
16 Branches
51 Contributors
Updated on Jun 30, 2025
Minified
Minified + Gzipped
Latest Version
1.0.35
Package Id
websocket@1.0.35
Unpacked Size
150.71 kB
Size
39.78 kB
File Count
23
NPM Version
10.2.4
Node Version
20.11.0
Published on
May 12, 2024
Cumulative downloads
Total Downloads
Last Day
-34.9%
69,060
Compared to previous day
Last Week
-15.3%
1,083,352
Compared to previous week
Last Month
8%
4,849,500
Compared to previous month
Last Year
2.9%
47,809,528
Compared to previous year
6
This is a (mostly) pure JavaScript implementation of the WebSocket protocol versions 8 and 13 for Node. There are some example client and server applications that implement various interoperability testing protocols in the "test/scripts" folder.
You can read the full API documentation in the docs folder.
Current Version: 1.0.35 - Release 2024-05-12
All current browsers are fully* supported.
(Not all W3C WebSocket features are supported by browsers. More info in the Full API documentation)
There are some basic benchmarking sections in the Autobahn test suite. I've put up a benchmark page that shows the results from the Autobahn tests run against AutobahnServer 0.4.10, WebSocket-Node 1.0.2, WebSocket-Node 1.0.4, and ws 0.3.4.
(These benchmarks are quite a bit outdated at this point, so take them with a grain of salt. Anyone up for running new benchmarks? I'll link to your report.)
The very complete Autobahn Test Suite is used by most WebSocket implementations to test spec compliance and interoperability.
In your project root:
$ npm install websocket
Then in your code:
1var WebSocketServer = require('websocket').server; 2var WebSocketClient = require('websocket').client; 3var WebSocketFrame = require('websocket').frame; 4var WebSocketRouter = require('websocket').router; 5var W3CWebSocket = require('websocket').w3cwebsocket;
W3CWebSocket
class).Here's a short example showing a server that echos back anything sent to it, whether utf-8 or binary.
1#!/usr/bin/env node 2var WebSocketServer = require('websocket').server; 3var http = require('http'); 4 5var server = http.createServer(function(request, response) { 6 console.log((new Date()) + ' Received request for ' + request.url); 7 response.writeHead(404); 8 response.end(); 9}); 10server.listen(8080, function() { 11 console.log((new Date()) + ' Server is listening on port 8080'); 12}); 13 14wsServer = new WebSocketServer({ 15 httpServer: server, 16 // You should not use autoAcceptConnections for production 17 // applications, as it defeats all standard cross-origin protection 18 // facilities built into the protocol and the browser. You should 19 // *always* verify the connection's origin and decide whether or not 20 // to accept it. 21 autoAcceptConnections: false 22}); 23 24function originIsAllowed(origin) { 25 // put logic here to detect whether the specified origin is allowed. 26 return true; 27} 28 29wsServer.on('request', function(request) { 30 if (!originIsAllowed(request.origin)) { 31 // Make sure we only accept requests from an allowed origin 32 request.reject(); 33 console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.'); 34 return; 35 } 36 37 var connection = request.accept('echo-protocol', request.origin); 38 console.log((new Date()) + ' Connection accepted.'); 39 connection.on('message', function(message) { 40 if (message.type === 'utf8') { 41 console.log('Received Message: ' + message.utf8Data); 42 connection.sendUTF(message.utf8Data); 43 } 44 else if (message.type === 'binary') { 45 console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); 46 connection.sendBytes(message.binaryData); 47 } 48 }); 49 connection.on('close', function(reasonCode, description) { 50 console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); 51 }); 52});
This is a simple example client that will print out any utf-8 messages it receives on the console, and periodically sends a random number.
This code demonstrates a client in Node.js, not in the browser
1#!/usr/bin/env node 2var WebSocketClient = require('websocket').client; 3 4var client = new WebSocketClient(); 5 6client.on('connectFailed', function(error) { 7 console.log('Connect Error: ' + error.toString()); 8}); 9 10client.on('connect', function(connection) { 11 console.log('WebSocket Client Connected'); 12 connection.on('error', function(error) { 13 console.log("Connection Error: " + error.toString()); 14 }); 15 connection.on('close', function() { 16 console.log('echo-protocol Connection Closed'); 17 }); 18 connection.on('message', function(message) { 19 if (message.type === 'utf8') { 20 console.log("Received: '" + message.utf8Data + "'"); 21 } 22 }); 23 24 function sendNumber() { 25 if (connection.connected) { 26 var number = Math.round(Math.random() * 0xFFFFFF); 27 connection.sendUTF(number.toString()); 28 setTimeout(sendNumber, 1000); 29 } 30 } 31 sendNumber(); 32}); 33 34client.connect('ws://localhost:8080/', 'echo-protocol');
Same example as above but using the W3C WebSocket API.
1var W3CWebSocket = require('websocket').w3cwebsocket; 2 3var client = new W3CWebSocket('ws://localhost:8080/', 'echo-protocol'); 4 5client.onerror = function() { 6 console.log('Connection Error'); 7}; 8 9client.onopen = function() { 10 console.log('WebSocket Client Connected'); 11 12 function sendNumber() { 13 if (client.readyState === client.OPEN) { 14 var number = Math.round(Math.random() * 0xFFFFFF); 15 client.send(number.toString()); 16 setTimeout(sendNumber, 1000); 17 } 18 } 19 sendNumber(); 20}; 21 22client.onclose = function() { 23 console.log('echo-protocol Client Closed'); 24}; 25 26client.onmessage = function(e) { 27 if (typeof e.data === 'string') { 28 console.log("Received: '" + e.data + "'"); 29 } 30};
For an example of using the request router, see libwebsockets-test-server.js
in the test
folder.
A presentation on the state of the WebSockets protocol that I gave on July 23, 2011 at the LA Hacker News meetup. WebSockets: The Real-Time Web, Delivered
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
8 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 6
Reason
Found 6/16 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More