{"record":{"id":"faf44bf3c8abae4a","repo":"grafana/k6","slug":"unknown-event-type-s","errorCode":null,"errorMessage":"unknown event type: %s","messagePattern":"unknown event type: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/websockets/listeners.go","lineNumber":99,"sourceCode":"\tcase events.ERROR:\n\t\treturn l.error\n\tcase events.CLOSE:\n\t\treturn l.close\n\tcase events.PING:\n\t\treturn l.ping\n\tcase events.PONG:\n\t\treturn l.pong\n\tdefault:\n\t\treturn nil\n\t}\n}\n\n// add adds a listener to the listeners\nfunc (l *eventListeners) add(t string, f func(sobek.Value) (sobek.Value, error)) error {\n\tlist := l.getType(t)\n\n\tif list == nil {\n\t\treturn fmt.Errorf(\"unknown event type: %s\", t)\n\t}\n\n\tlist.add(f)\n\n\treturn nil\n}\n\n// all returns all possible listeners for a certain event type or an empty array\nfunc (l *eventListeners) all(t string) []func(sobek.Value) (sobek.Value, error) {\n\tlist := l.getType(t)\n\n\tif list == nil {\n\t\treturn []func(sobek.Value) (sobek.Value, error){}\n\t}\n\n\treturn list.all()\n}\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/websockets/listeners.go#L81-L117","documentation":"The WebSocket event registry only recognizes six event types — open, message, error, close, ping and pong (events.OPEN/MESSAGE/ERROR/CLOSE/PING/PONG). addEventListener (and the on* setters routing through the same registry) return this error when handed any other string, because getType returns nil and add refuses to create unknown categories.","triggerScenarios":"ws.addEventListener('data', fn), socket.on('connection', fn), addEventListener('pong ' with trailing space or wrong casing like 'Open'), or browser-only event names ('addEventListener('open', ...)' is fine but 'addEventListener('reconnect', ...)' is not). The error is returned from add and thrown to the script by the caller.","commonSituations":"Porting Node.js 'ws' or browser WebSocket/EventSource code that listens for events k6 does not emit ('unexpected-response', 'upgrade', 'message-error'); typo'd or case-mismatched names; expecting the HTTP-layer events of other tools; scripts written for socket.io-style APIs.","solutions":["Use only open, message, error, close, ping, pong — exactly lowercase","For connection failures, handle 'error' and 'close' instead of looking for 'reconnect'/'connection' events","Double-check spelling/casing: addEventListener('Close') fails, 'close' works","Use the on* convenience properties (socket.onopen etc.) which map to the same registry and are harder to mistype"],"exampleFix":"// before\nsocket.addEventListener('data', (ev) => { ... }); // unknown event type: data\n\n// after\nsocket.addEventListener('message', (ev) => {\n  const data = ev.data(); // payloads arrive on 'message'\n});","handlingStrategy":"validation","validationCode":"const WS_EVENTS = new Set(['open','message','error','close','ping','pong']);\nfunction addListener(sock, type, fn) {\n  if (!WS_EVENTS.has(type)) throw new TypeError(`unknown WebSocket event '${type}'; valid: ${[...WS_EVENTS].join(', ')}`);\n  if (typeof fn !== 'function') throw new TypeError('listener must be a function');\n  sock.addEventListener(type, fn);\n}","typeGuard":"const isWsEventName = t => ['open','message','error','close','ping','pong'].includes(t);","tryCatchPattern":"try { sock.addEventListener(type, fn); } catch (e) { if (/unknown event type/.test(e.message)) console.warn(`skipping unsupported event ${type}`); else throw e; }","preventionTips":["Memorize the six supported events: open, message, error, close, ping, pong","Use lowercase names only — k6 matches exactly","Handle connection problems via 'error'/'close'; there is no 'reconnect' or 'connection' event","Centralize addEventListener calls through one helper that validates the event name"],"tags":["websockets","events","api-mismatch","node-porting"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}