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
A WebSocket Implementation for Node.JS (Draft -08 through the final RFC 6455)
npm install websocket
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,764 Stars
539 Commits
602 Forks
143 Watching
7 Branches
51 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
JavaScript (92.74%)
HTML (5.33%)
EJS (1.14%)
CSS (0.53%)
Shell (0.18%)
Makefile (0.09%)
Cumulative downloads
Total Downloads
Last day
6.1%
190,118
Compared to previous day
Last week
16.6%
1,093,897
Compared to previous week
Last month
12.6%
4,018,974
Compared to previous month
Last year
-6.5%
44,630,423
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.34 - Release 2021-04-14
globalThis
property when available. See this MDN page for context. Resolves #415All 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
Found 10/16 approved changesets -- score normalized to 6
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
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 2024-11-18
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