grafana/k6 · error

missing argument, expected ArrayBuffer

Error message

missing argument, expected ArrayBuffer

What it means

Thrown by Socket.sendBinary() in the legacy k6/ws module when it is called with no argument at all. The Go method (internal/js/modules/k6/ws/ws.go:348) receives a nil sobek value for a missing argument and immediately throws 'missing argument, expected ArrayBuffer'. A present argument of the wrong type produces a different error naming the unsupported type.

Solutions

  1. Pass an ArrayBuffer: socket.sendBinary(new Uint8Array([1, 2, 3]).buffer)
  2. If the payload is a view over a larger buffer, slice first: new Uint8Array(buf, byteOffset, length).slice().buffer
  3. Prefer socket.send(...) for strings/objects — sendBinary is specifically the ArrayBuffer path

Example fix

// before
socket.sendBinary();

// after
socket.sendBinary(new Uint8Array([1, 2, 3]).buffer);
Defensive patterns

Strategy: validation

Validate before calling

function sendBinaryChecked(socket, payload) {
  if (payload === undefined || payload === null) {
    throw new TypeError('sendBinary requires an ArrayBuffer, e.g. new Uint8Array([...]).buffer');
  }
  if (!(payload instanceof ArrayBuffer)) {
    payload = payload instanceof Uint8Array ? payload.slice().buffer : null;
    if (payload === null) throw new TypeError('sendBinary requires an ArrayBuffer');
  }
  socket.sendBinary(payload);
}

Type guard

function isArrayBuffer(v) {
  return v instanceof ArrayBuffer;
}

Prevention

When it happens

Trigger: socket.sendBinary() with empty parentheses inside the ws.connect setup callback — e.g. refactoring dropped the buffer argument, or the variable holding the ArrayBuffer is not in scope and the call was left bare.

Common situations: Migrating scripts that used socket.send(...) with defaults; refactoring that moves the payload into another function while leaving the call site argumentless; copy-paste of the method name without the payload.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/108fdbbba6ef78f4. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/ws/ws.go:348

	if err := s.conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
		s.handleEvent("error", s.rt.ToValue(err))
	}

	metrics.PushIfNotDone(s.ctx, s.samplesOutput, metrics.Sample{
		TimeSeries: metrics.TimeSeries{
			Metric: s.builtinMetrics.WSMessagesSent,
			Tags:   s.tagsAndMeta.Tags,
		},
		Time:     time.Now(),
		Metadata: s.tagsAndMeta.Metadata,
		Value:    1,
	})
}

// SendBinary writes the given ArrayBuffer message to the connection.
func (s *Socket) SendBinary(message sobek.Value) {
	if message == nil {
		common.Throw(s.rt, errors.New("missing argument, expected ArrayBuffer"))
	}

	msg := message.Export()
	if ab, ok := msg.(sobek.ArrayBuffer); ok {
		if err := s.conn.WriteMessage(websocket.BinaryMessage, ab.Bytes()); err != nil {
			s.handleEvent("error", s.rt.ToValue(err))
		}
	} else {
		var jsType string
		switch {
		case sobek.IsNull(message), sobek.IsUndefined(message):
			jsType = message.String()
		default:
			jsType = message.ToObject(s.rt).Get("constructor").ToObject(s.rt).Get("name").String()
		}
		common.Throw(s.rt, fmt.Errorf("expected ArrayBuffer as argument, received: %s", jsType))
	}

View on GitHub (pinned to 01ffac6f24)