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.2.3
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.11.0
Choose a head ref
Loading
6 changes: 5 additions & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'Bug report'
name: Bug report
description: Create a bug report
body:
- type: markdown
@@ -12,6 +12,10 @@ body:
Please fill in as much of the template below as you're able.
- type: checkboxes
attributes:
label: Is there an existing issue for this?
description:
Please search to see if an issue already exists for the bug you
encountered.
options:
- label:
I've searched for any related issues and avoided creating a
18 changes: 13 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ on:
- push
- pull_request

permissions: {}

jobs:
test:
runs-on: ${{ matrix.os }}
@@ -17,6 +19,8 @@ jobs:
- 12
- 14
- 16
- 18
- 19
os:
- macOS-latest
- ubuntu-latest
@@ -26,9 +30,12 @@ jobs:
os: macOS-latest
- arch: x86
os: ubuntu-latest
- arch: x86
node: 18
os: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
architecture: ${{ matrix.arch }}
@@ -38,9 +45,10 @@ jobs:
matrix.os == 'ubuntu-latest' && matrix.node == 16 && matrix.arch ==
'x64'
- run: npm test
- run:
echo ::set-output name=job_id::$(node -e
"console.log(crypto.randomBytes(16).toString('hex'))")
- run: |
id=$(node -e "console.log(crypto.randomBytes(16).toString('hex'))")
echo "job_id=$id" >> $GITHUB_OUTPUT
id: get_job_id
shell: bash
- uses: coverallsapp/github-action@1.1.3
52 changes: 27 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ can use one of the many wrappers available on npm, like
- [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
- [Client authentication](#client-authentication)
- [Server broadcast](#server-broadcast)
- [echo.websocket.org demo](#echowebsocketorg-demo)
- [Round-trip time](#round-trip-time)
- [Use the Node.js streams API](#use-the-nodejs-streams-api)
- [Other examples](#other-examples)
- [FAQ](#faq)
@@ -68,6 +68,13 @@ necessarily need to have a C++ compiler installed on your machine.
- `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
message contains valid UTF-8.

To not even try to require and use these modules, use the
[`WS_NO_BUFFER_UTIL`](./doc/ws.md#ws_no_buffer_util) and
[`WS_NO_UTF_8_VALIDATE`](./doc/ws.md#ws_no_utf_8_validate) environment
variables. These might be useful to enhance security in systems where a user can
put a package in the package search path of an application of another user, due
to how the Node.js resolver algorithm works.

## API docs

See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
@@ -148,8 +155,8 @@ ws.on('open', function open() {
ws.send('something');
});

ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.on('message', function message(data) {
console.log('received: %s', data);
});
```

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

wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.on('message', function message(data) {
console.log('received: %s', data);
});

ws.send('something');
@@ -201,8 +208,8 @@ const server = createServer({
const wss = new WebSocketServer({ server });

wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.on('message', function message(data) {
console.log('received: %s', data);
});

ws.send('something');
@@ -252,21 +259,21 @@ server.listen(8080);
### Client authentication

```js
import WebSocket from 'ws';
import { createServer } from 'http';
import { WebSocketServer } from 'ws';

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

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

server.on('upgrade', function upgrade(request, socket, head) {
// This function is not defined on purpose. Implement it with your own logic.
authenticate(request, (err, client) => {
authenticate(request, function next(err, client) {
if (err || !client) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
@@ -295,7 +302,7 @@ import WebSocket, { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('message', function incoming(data, isBinary) {
ws.on('message', function message(data, isBinary) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data, { binary: isBinary });
@@ -314,7 +321,7 @@ import WebSocket, { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.on('message', function incoming(data, isBinary) {
ws.on('message', function message(data, isBinary) {
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data, { binary: isBinary });
@@ -324,14 +331,12 @@ wss.on('connection', function connection(ws) {
});
```

### echo.websocket.org demo
### Round-trip time

```js
import WebSocket from 'ws';

const ws = new WebSocket('wss://echo.websocket.org/', {
origin: 'https://websocket.org'
});
const ws = new WebSocket('wss://websocket-echo.com/');

ws.on('open', function open() {
console.log('connected');
@@ -342,8 +347,8 @@ ws.on('close', function close() {
console.log('disconnected');
});

ws.on('message', function incoming(data) {
console.log(`Roundtrip time: ${Date.now() - data} ms`);
ws.on('message', function message(data) {
console.log(`Round-trip time: ${Date.now() - data} ms`);

setTimeout(function timeout() {
ws.send(Date.now());
@@ -356,9 +361,7 @@ ws.on('message', function incoming(data) {
```js
import WebSocket, { createWebSocketStream } from 'ws';

const ws = new WebSocket('wss://echo.websocket.org/', {
origin: 'https://websocket.org'
});
const ws = new WebSocket('wss://websocket-echo.com/');

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

@@ -457,7 +460,7 @@ function heartbeat() {
}, 30000 + 1000);
}

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

client.on('open', heartbeat);
client.on('ping', heartbeat);
@@ -489,5 +492,4 @@ We're using the GitHub [releases][changelog] for changelog entries.
[server-report]: http://websockets.github.io/ws/autobahn/servers/
[session-parse-example]: ./examples/express-session-parse
[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
[ws-server-options]:
https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
[ws-server-options]: ./doc/ws.md#new-websocketserveroptions-callback
Loading