property

ServerWebSocket.binaryType

binaryType?: 'arraybuffer' | 'blob' | 'uint8array' | 'nodebuffer'

Sets how binary data is returned in events.

  • if nodebuffer, binary data is returned as Buffer objects. (default)
  • if arraybuffer, binary data is returned as ArrayBuffer objects.
  • if uint8array, binary data is returned as Uint8Array objects.
  • if blob, binary data is returned as Blob objects.

This applies to binary messages and to the payloads of pings and pongs.

Bun.serve({
  websocket: {
    open(ws) {
      ws.binaryType = "uint8array";
    },
    message(ws, message) {
      console.log(message instanceof Uint8Array); // true, for a binary message
    },
  },
  // ...
});