How can send binary message via websocket in K6

Dear K6 support,

According to Socket.send(data), k6 websocket only send string data.
Our websocket server is using google protobuf instead of JSON, according to Using protobuf with k6 - #2 by mstoykov, I can send message to our websocket server, but it will convert binary data to string.

var message = message_proto.serializeBinary()

Is there any solution?


Hi @royzhang007,
You can read this thread, unfortunately @vinzy never published a PR with the change, probably forgot as there was currently a PR that would’ve made that harder, and it was forgotten :(. We can probably add it at some later point after 1007 is done.

You are also free to make a PR with adding this :smiley: , or at least open an issue

Thx @mstoykov!

How ever seems it doesn’t work. Could u please kindly help to have a look? Thx!!!

js/modules/k6/ws/ws.go:340:2: s.msgSentTimestamps undefined (type *Socket has no field or method msgSentTimestamps)

Dear @mstoykov,

I use the following code instead and it can do the “go install”.

func (s *Socket) SendBinary(writeData []byte) {
// NOTE: No binary message support for the time being since goja doesn’t
// support typed arrays.
rt := common.GetRuntime(s.ctx)

    if err := s.conn.WriteMessage(websocket.BinaryMessage, writeData); err != nil {
            s.handleEvent("error", rt.ToValue(err))

    stats.PushIfNotDone(s.ctx, s.samplesOutput, stats.Sample{
            Metric: metrics.WSMessagesSent,
            Time:   time.Now(),
            Tags:   s.sampleTags,
            Value:  1,


But when I call the sendBinary(message) method, it throw the following error. My message’s type is Uint8Array which is returned by google protobuf js. How can I resolve this issue? Really appreciate ur great help in advance!

ERRO[0001] TypeError: Could not convert function call parameter 24,184,181,178,181,161,46,34,11,99,108,97,117,100,105,111,55,55,55,55 to []uint8
at native

The following is message_proto.serializeBinary() method


  • Serializes the message to binary data (in protobuf wire format).
  • @return {!Uint8Array}
    proto.ControlMessage.prototype.serializeBinary = function() {
    var writer = new jspb.BinaryWriter();
    proto.ControlMessage.serializeBinaryToWriter(this, writer);
    return writer.getResultBuffer();


hmm if you use that specific patch to k6, you shouldn’t pass the Uint8Array to sendBinary(), you should make an array of its ints and use that instead. k6 doesn’t support user-generated binary data well yet (see #1020), so until we do, such nasty hacks are required.