http,inspector: add WebSocket upgrade observability#62933
http,inspector: add WebSocket upgrade observability#62933GrinZero wants to merge 1 commit intonodejs:mainfrom
Conversation
|
Review requested:
|
const Koa = require('koa')
const Router = require('koa-router')
const WebSocket = require('ws')
const app = new Koa()
const router = new Router()
let ws
let opened
function ensureWebSocket() {
if (ws && ws.readyState === WebSocket.OPEN) return
Promise.resolve()
ws = new WebSocket('wss://echo.websocket.org/')
opened = new Promise((resolve) => ws.once('open', resolve))
ws.on('message', (data) => {
console.log('WebSocket message:', data.toString())
})
ws.on('close', () => {
console.log('WebSocket connection closed')
ws = null
opened = null
})
return opened
}
router.get('/ws', async (ctx) => {
await ensureWebSocket()
ws.send(ctx.query.message || 'Hello from Koa')
ctx.body = 'WebSocket: Send'
})
app.use(router.routes())
app.listen(3000, () => {
console.log('GET http://127.0.0.1:3000/ws?message=ping')
}) |
|
A slight push, hoping someone will notice. |
legendecas
left a comment
There was a problem hiding this comment.
I think we should focus on improving the built-in WebSocket API inspection at the moment, as in
node/lib/internal/inspector/network_undici.js
Lines 244 to 245 in f67cf47
😂Haha, you found it. Not without a plan, I actually have three PRs waiting to be implemented: native sse, fetch-based sse, and native webso. |
|
I wrote the manual analysis of the websocket protocol first because the new version is not yet widely circulated, and many low-level libraries still use the old version. |


Summary
This patch adds observability for HTTP client WebSocket upgrades through both
diagnostics_channeland the inspectorNetworkdomain.Today, regular HTTP client requests are surfaced through the existing
http.client.*lifecycle, but upgrade-based WebSocket traffic is not represented cleanly:This change fills that gap.
Changes
Network.webSocket*events for:Why This Approach
This is implemented at the HTTP upgrade / inspector plumbing layer rather than in a specific WebSocket client API.
That keeps the change aligned with where the underlying lifecycle actually occurs:
It also avoids coupling the observability model to any single user-facing API, making the behavior reusable for clients built on the same upgrade path.
Scope
This PR does not add special handling or declarations for the built-in
WebSocketorWebSocketStreamAPIs directly.Those APIs are a separate surface-area discussion. The goal here is narrower: add transport-level observability for WebSocket upgrades and frame traffic first, so higher-level clients can benefit from the same underlying inspector and diagnostics hooks without expanding this PR beyond that scope.
Testing
Automated
Manual e2e
Used a small local Koa app with a
/wsroute:/wsopens an outboundwsconnection towss://echo.websocket.org//ws?message=...Refs