web api instance property
WebSocket: binaryType property
Available in workers
The WebSocket.binaryType property controls the type of
binary data being received over the WebSocket connection.
Value
A string:
"blob"- : Use
Blobobjects for binary data. This is the default value.
- : Use
"arraybuffer"- : Use
ArrayBufferobjects for binary data.
- : Use
Examples
// 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
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.