WebSocket binaryType: Blob vs ArrayBuffer

When handling binary data through the JavaScript WebSocket API, the binaryType property determines whether incoming binary messages are received as a Blob or an ArrayBuffer. While Blob is optimized for handling immutable, large-scale file data with minimal memory overhead, ArrayBuffer provides direct, low-level access to raw memory buffers for byte-by-byte manipulation and custom binary protocols. Choosing the correct type depends on whether your application needs to read and modify raw binary packets in real time or simply pass large data streams directly to other web APIs.

Understanding the WebSocket binaryType Property

By default, the binaryType property of a WebSocket connection in browser environments is set to 'blob'. You can inspect or change this setting at any point after initializing a WebSocket connection:

const socket = new WebSocket('wss://example.com/socket');

// Set the binary type to ArrayBuffer
socket.binaryType = 'arraybuffer';

// Or set it to Blob (default)
socket.binaryType = 'blob';

When binary data arrives over the connection, the event.data payload in the onmessage handler will match the designated type.

What is a Blob?

A Blob (Binary Large Object) represents an immutable, raw chunk of binary data. Blobs are not stored directly in JavaScript memory; instead, they often reside on disk or in system memory managed by the browser engine.

Key Characteristics of Blob:

Common Use Cases:

socket.binaryType = 'blob';

socket.onmessage = (event) => {
  if (event.data instanceof Blob) {
    const imageUrl = URL.createObjectURL(event.data);
    document.querySelector('img').src = imageUrl;
  }
};

What is an ArrayBuffer?

An ArrayBuffer is a fixed-length, raw binary data buffer stored directly in JavaScript memory. Unlike a Blob, you can read, write, and manipulate individual bytes within an ArrayBuffer using TypedArray views (such as Uint8Array, Float32Array) or a DataView.

Key Characteristics of ArrayBuffer:

Common Use Cases:

socket.binaryType = 'arraybuffer';

socket.onmessage = (event) => {
  if (event.data instanceof ArrayBuffer) {
    const view = new DataView(event.data);
    const packetType = view.getUint8(0);
    const payloadValue = view.getInt32(1, true); // Little-endian
  }
};

Comparison: Blob vs ArrayBuffer

Feature Blob ArrayBuffer
Default Setting Yes (in browser environments) No
Data Mutability Immutable Mutable via TypedArrays and DataView
Byte Manipulation Asynchronous (requires FileReader or .arrayBuffer()) Synchronous and direct
Memory Footprint Low JS heap usage (stored in browser memory/disk) Higher JS heap usage (allocated directly in JS memory)
Best For Images, multimedia, file downloads Network packets, custom protocols, WebGL/Wasm

Summary

Use blob when you receive files or media that you plan to pass directly to HTML elements, object URLs, or storage APIs without inspecting the contents. Switch to arraybuffer when your application implements custom serialization, real-time gaming protocols, or requires instant, synchronous byte-level manipulation in memory.