Skip to content

Commit 0a61236

Browse files
adrianhopebailielpinca
authored andcommitted
[doc] Discourage use of verifyClient hook (#1613)
1 parent 91b5173 commit 0a61236

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ can use one of the many wrappers available on npm, like
3232
- [Simple server](#simple-server)
3333
- [External HTTP/S server](#external-https-server)
3434
- [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
35+
- [Client authentication](#client-authentication)
3536
- [Server broadcast](#server-broadcast)
3637
- [echo.websocket.org demo](#echowebsocketorg-demo)
3738
- [Use the Node.js streams API](#use-the-nodejs-streams-api)
@@ -249,6 +250,40 @@ server.on('upgrade', function upgrade(request, socket, head) {
249250
server.listen(8080);
250251
```
251252

253+
### Client authentication
254+
255+
```js
256+
const http = require('http');
257+
const WebSocket = require('ws');
258+
const url = require('url');
259+
260+
const server = http.createServer();
261+
const wss = new WebSocket.Server({ noServer: true });
262+
263+
wss.on('connection', function(ws, request, client) {
264+
ws.on('message', function(message) {
265+
console.log(`WS message ${message} from user ${client}`);
266+
});
267+
});
268+
269+
server.on('upgrade', function upgrade(request, socket, head) {
270+
authenticate(request, (err, client) => {
271+
if (err || !client) {
272+
socket.destroy();
273+
return;
274+
}
275+
wss.handleUpgrade(request, socket, head, function done(ws) {
276+
wss.emit('connection', ws, request, client);
277+
});
278+
});
279+
});
280+
281+
server.listen(8080);
282+
```
283+
284+
Also see the provided [example](./examples/express-session-parse) using
285+
`express-session`.
286+
252287
### Server broadcast
253288

254289
A client WebSocket broadcasting to all connected WebSocket clients, including

doc/ws.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ This class represents a WebSocket server. It extends the `EventEmitter`.
5757
- `backlog` {Number} The maximum length of the queue of pending connections.
5858
- `server` {http.Server|https.Server} A pre-created Node.js HTTP/S server.
5959
- `verifyClient` {Function} A function which can be used to validate incoming
60-
connections. See description below.
60+
connections. See description below. (Usage is discouraged: see
61+
[Issue #337](https://github.com/websockets/ws/issues/377#issuecomment-462152231))
6162
- `handleProtocols` {Function} A function which can be used to handle the
6263
WebSocket subprotocols. See description below.
6364
- `path` {String} Accept only connections matching this path.
@@ -75,6 +76,10 @@ started manually. The "noServer" mode allows the WebSocket server to be
7576
completly detached from the HTTP/S server. This makes it possible, for example,
7677
to share a single HTTP/S server between multiple WebSocket servers.
7778

79+
> **NOTE:** Use of `verifyClient` is discouraged. Rather handle client
80+
> authentication in the `upgrade` event of the HTTP server. See examples for
81+
> more details.
82+
7883
If `verifyClient` is not set then the handshake is automatically accepted. If it
7984
is provided with a single argument then that is:
8085

examples/express-session-parse/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ app.delete('/logout', function(request, response) {
4848
//
4949
const server = http.createServer(app);
5050

51-
const wss = new WebSocket.Server({
52-
verifyClient: function(info, done) {
53-
console.log('Parsing session from request...');
54-
sessionParser(info.req, {}, () => {
55-
console.log('Session is parsed!');
51+
const wss = new WebSocket.Server({ noServer: true });
5652

57-
//
58-
// We can reject the connection by returning false to done(). For example,
59-
// reject here if user is unknown.
60-
//
61-
done(info.req.session.userId);
53+
server.on('upgrade', function upgrade(request, socket, head) {
54+
console.log('Parsing session from request...');
55+
sessionParser(request, {}, () => {
56+
if (!request.session.userId) {
57+
socket.destroy();
58+
return;
59+
}
60+
console.log('Session is parsed!');
61+
wss.handleUpgrade(request, socket, head, function done(ws) {
62+
wss.emit('connection', ws, request);
6263
});
63-
},
64-
server
64+
});
6565
});
6666

6767
wss.on('connection', function(ws, request) {

examples/express-session-parse/public/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(function() {
22
const messages = document.querySelector('#messages');
33
const wsButton = document.querySelector('#wsButton');
4+
const wsSendButton = document.querySelector('#wsSendButton');
45
const logout = document.querySelector('#logout');
56
const login = document.querySelector('#login');
67

@@ -50,6 +51,16 @@
5051
};
5152
ws.onclose = function() {
5253
showMessage('WebSocket connection closed');
54+
ws = null;
5355
};
5456
};
57+
58+
wsSendButton.onclick = function() {
59+
if (!ws) {
60+
showMessage('No WebSocket connection');
61+
return;
62+
}
63+
ws.send('Hello World!');
64+
showMessage('Sent "Hello World!"');
65+
};
5566
})();

examples/express-session-parse/public/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ <h1>Choose an action.</h1>
1515
<button id="wsButton" type="button" title="Open WebSocket connection">
1616
Open WebSocket connection
1717
</button>
18+
<button id="wsSendButton" type="button" title="Send WebSocket message">
19+
Send WebSocket message
20+
</button>
1821
<pre id="messages" style="height: 400px; overflow: scroll"></pre>
1922
<script src="app.js"></script>
2023
</body>

0 commit comments

Comments
 (0)