WebSocket: binaryType property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Note: This feature is available in Web Workers.

The WebSocket.binaryType property controls the type of binary data being received over the WebSocket connection.

Value

A string:

"blob"

Use Blob objects for binary data. This is the default value.

"arraybuffer"

Use ArrayBuffer objects for binary data.

Examples

js
// Create WebSocket connection.
const socket = new WebSocket("ws://localhost:8080");

// Change binary type from "blob" to "arraybuffer"
socket.binaryType = "arraybuffer";

// Listen for messages
socket.addEventListener("message", (event) => {
  if (event.data instanceof ArrayBuffer) {
    // binary frame
    const view = new DataView(event.data);
    console.log(view.getInt32(0));
  } else {
    // text frame
    console.log(event.data);
  }
});

Specifications

Specification
WebSockets
# ref-for-dom-websocket-binarytype①

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
binaryType

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support