The WebSocket.binaryType
property controls the type of binary data being received over the WebSocket connection.
On this page
WebSocket: binaryType property
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 Standard # ref-for-dom-websocket-binarytype① |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
binaryType |
15 | 12 | 11 | 10 | 12.1 | 6 | 4.4 | 18 | 14 | 12.1 | 6 | 1.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType