Performance: Authentication in WebSocket connections
MEDIUM IMPACT
This affects the initial connection setup time and ongoing message handling responsiveness in WebSocket communication.
const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080, verifyClient: (info, done) => { const token = extractTokenFromInfo(info); if (authenticate(token)) { done(true); } else { done(false, 401, 'Unauthorized'); } } }); wss.on('connection', function connection(ws) { // User is authenticated once during handshake ws.on('message', function incoming(message) { // Process message directly }); });
const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { // Authenticate user on every message if (!authenticate(message.token)) { ws.close(); return; } // Process message }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Authenticate on every message | N/A (WebSocket messages) | N/A | N/A | [X] Bad |
| Authenticate once during handshake | N/A (WebSocket messages) | N/A | N/A | [OK] Good |