Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: websockets/ws
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8.12.0
Choose a base ref
...
head repository: websockets/ws
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8.12.1
Choose a head ref
  • 3 commits
  • 5 files changed
  • 2 contributors

Commits on Jan 23, 2023

  1. Copy the full SHA
    2862c2f View commit details

Commits on Feb 13, 2023

  1. [pkg] Add browser condition (#2118)

    Fixes #2117
    flashd2n authored Feb 13, 2023
    Copy the full SHA
    0d114ef View commit details
  2. [dist] 8.12.1

    lpinca committed Feb 13, 2023
    Copy the full SHA
    a04578e View commit details
Showing with 54 additions and 1 deletion.
  1. +36 −0 README.md
  2. +10 −0 examples/express-session-parse/index.js
  3. +2 −0 examples/server-stats/index.js
  4. +4 −0 examples/ssl.js
  5. +2 −1 package.json
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -155,6 +155,8 @@ import WebSocket from 'ws';

const ws = new WebSocket('ws://www.host.com/path');

ws.on('error', console.error);

ws.on('open', function open() {
ws.send('something');
});
@@ -171,6 +173,8 @@ import WebSocket from 'ws';

const ws = new WebSocket('ws://www.host.com/path');

ws.on('error', console.error);

ws.on('open', function open() {
const array = new Float32Array(5);

@@ -190,6 +194,8 @@ import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data) {
console.log('received: %s', data);
});
@@ -212,6 +218,8 @@ const server = createServer({
const wss = new WebSocketServer({ server });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data) {
console.log('received: %s', data);
});
@@ -234,10 +242,14 @@ const wss1 = new WebSocketServer({ noServer: true });
const wss2 = new WebSocketServer({ noServer: true });

wss1.on('connection', function connection(ws) {
ws.on('error', console.error);

// ...
});

wss2.on('connection', function connection(ws) {
ws.on('error', console.error);

// ...
});

@@ -266,16 +278,24 @@ server.listen(8080);
import { createServer } from 'http';
import { WebSocketServer } from 'ws';

function onSocketError(err) {
console.error(err);
}

const server = createServer();
const wss = new WebSocketServer({ noServer: true });

wss.on('connection', function connection(ws, request, client) {
ws.on('error', console.error);

ws.on('message', function message(data) {
console.log(`Received message ${data} from user ${client}`);
});
});

server.on('upgrade', function upgrade(request, socket, head) {
socket.on('error', onSocketError);

// This function is not defined on purpose. Implement it with your own logic.
authenticate(request, function next(err, client) {
if (err || !client) {
@@ -284,6 +304,8 @@ server.on('upgrade', function upgrade(request, socket, head) {
return;
}

socket.removeListener('error', onSocketError);

wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request, client);
});
@@ -306,6 +328,8 @@ import WebSocket, { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data, isBinary) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
@@ -325,6 +349,8 @@ import WebSocket, { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(data, isBinary) {
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
@@ -342,6 +368,8 @@ import WebSocket from 'ws';

const ws = new WebSocket('wss://websocket-echo.com/');

ws.on('error', console.error);

ws.on('open', function open() {
console.log('connected');
ws.send(Date.now());
@@ -369,6 +397,8 @@ const ws = new WebSocket('wss://websocket-echo.com/');

const duplex = createWebSocketStream(ws, { encoding: 'utf8' });

duplex.on('error', console.error);

duplex.pipe(process.stdout);
process.stdin.pipe(duplex);
```
@@ -393,6 +423,8 @@ const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws, req) {
const ip = req.socket.remoteAddress;

ws.on('error', console.error);
});
```

@@ -402,6 +434,8 @@ the `X-Forwarded-For` header.
```js
wss.on('connection', function connection(ws, req) {
const ip = req.headers['x-forwarded-for'].split(',')[0].trim();

ws.on('error', console.error);
});
```

@@ -425,6 +459,7 @@ const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.on('error', console.error);
ws.on('pong', heartbeat);
});

@@ -466,6 +501,7 @@ function heartbeat() {

const client = new WebSocket('wss://websocket-echo.com/');

client.on('error', console.error);
client.on('open', heartbeat);
client.on('ping', heartbeat);
client.on('close', function clear() {
10 changes: 10 additions & 0 deletions examples/express-session-parse/index.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,10 @@ const uuid = require('uuid');

const { WebSocketServer } = require('../..');

function onSocketError(err) {
console.error(err);
}

const app = express();
const map = new Map();

@@ -59,6 +63,8 @@ const server = http.createServer(app);
const wss = new WebSocketServer({ clientTracking: false, noServer: true });

server.on('upgrade', function (request, socket, head) {
socket.on('error', onSocketError);

console.log('Parsing session from request...');

sessionParser(request, {}, () => {
@@ -70,6 +76,8 @@ server.on('upgrade', function (request, socket, head) {

console.log('Session is parsed!');

socket.removeListener('error', onSocketError);

wss.handleUpgrade(request, socket, head, function (ws) {
wss.emit('connection', ws, request);
});
@@ -81,6 +89,8 @@ wss.on('connection', function (ws, request) {

map.set(userId, ws);

ws.on('error', console.error);

ws.on('message', function (message) {
//
// Here we can now use session parameters.
2 changes: 2 additions & 0 deletions examples/server-stats/index.js
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ wss.on('connection', function (ws) {
}, 100);
console.log('started client interval');

ws.on('error', console.error);

ws.on('close', function () {
console.log('stopping client interval');
clearInterval(id);
4 changes: 4 additions & 0 deletions examples/ssl.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ const server = https.createServer({
const wss = new WebSocketServer({ server });

wss.on('connection', function connection(ws) {
ws.on('error', console.error);

ws.on('message', function message(msg) {
console.log(msg.toString());
});
@@ -31,6 +33,8 @@ server.listen(function listening() {
rejectUnauthorized: false
});

ws.on('error', console.error);

ws.on('open', function open() {
ws.send('All glory to WebSockets!');
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ws",
"version": "8.12.0",
"version": "8.12.1",
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
"keywords": [
"HyBi",
@@ -18,6 +18,7 @@
"main": "index.js",
"exports": {
".": {
"browser": "./browser.js",
"import": "./wrapper.mjs",
"require": "./index.js"
},