grafana/k6 · error

expected ArrayBuffer as argument, received: %s

Error message

expected ArrayBuffer as argument, received: %s

What it means

Thrown by ws.Socket.SendBinary when the argument exports to something other than a sobek.ArrayBuffer. The method first reports the missing-argument case separately; this error fires when a value was provided but is of the wrong type — the message includes the JS-side constructor name (or null/undefined) so you can see what was passed instead. The socket write is never attempted.

Source

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

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))
	}

	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,
	})
}

// Ping sends a ping message over the websocket.
func (s *Socket) Ping() {
	deadline := time.Now().Add(writeWait)
	pingID := strconv.Itoa(s.pingSendCounter)
	data := []byte(pingID)

View on GitHub (pinned to 01ffac6f24)

Solutions

  1. Pass an ArrayBuffer: socket.sendBinary(new TextEncoder().encode(data).buffer)
  2. For a TypedArray, pass its .buffer property
  3. Use socket.send() for string messages
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at internal/js/modules/k6/ws/ws.go:364 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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