grafana/k6 · error

unknown event type: %s

Error message

unknown event type: %s

What it means

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.

Source

Thrown at internal/js/modules/k6/websockets/listeners.go:99

	case events.ERROR:
		return l.error
	case events.CLOSE:
		return l.close
	case events.PING:
		return l.ping
	case events.PONG:
		return l.pong
	default:
		return nil
	}
}

// add adds a listener to the listeners
func (l *eventListeners) add(t string, f func(sobek.Value) (sobek.Value, error)) error {
	list := l.getType(t)

	if list == nil {
		return fmt.Errorf("unknown event type: %s", t)
	}

	list.add(f)

	return nil
}

// all returns all possible listeners for a certain event type or an empty array
func (l *eventListeners) all(t string) []func(sobek.Value) (sobek.Value, error) {
	list := l.getType(t)

	if list == nil {
		return []func(sobek.Value) (sobek.Value, error){}
	}

	return list.all()
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Use only open, message, error, close, ping, pong — exactly lowercase
  2. For connection failures, handle 'error' and 'close' instead of looking for 'reconnect'/'connection' events
  3. Double-check spelling/casing: addEventListener('Close') fails, 'close' works
  4. Use the on* convenience properties (socket.onopen etc.) which map to the same registry and are harder to mistype

Example fix

// before
socket.addEventListener('data', (ev) => { ... }); // unknown event type: data

// after
socket.addEventListener('message', (ev) => {
  const data = ev.data(); // payloads arrive on 'message'
});
Defensive patterns

Strategy: validation

Validate before calling

const WS_EVENTS = new Set(['open','message','error','close','ping','pong']);
function addListener(sock, type, fn) {
  if (!WS_EVENTS.has(type)) throw new TypeError(`unknown WebSocket event '${type}'; valid: ${[...WS_EVENTS].join(', ')}`);
  if (typeof fn !== 'function') throw new TypeError('listener must be a function');
  sock.addEventListener(type, fn);
}

Type guard

const isWsEventName = t => ['open','message','error','close','ping','pong'].includes(t);

Try / catch

try { sock.addEventListener(type, fn); } catch (e) { if (/unknown event type/.test(e.message)) console.warn(`skipping unsupported event ${type}`); else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/faf44bf3c8abae4a. Report an issue: GitHub.