Why binary at all
JSON is human-readable, debuggable, and slow. For high-frequency or compact messages — multiplayer game positions, market ticks, sensor streams — binary cuts payload size by 60-80% and parse time even more. The browser supports binary natively; the cost is that you write your own encoder/decoder.
DataView is your friend
DataView reads and writes typed values at byte offsets in an ArrayBuffer. Pick endianness explicitly (almost always pass true for little-endian) so the same wire format works between your JS client and your Python server. Combine multiple fields into one buffer with known offsets and you have a hand-rolled binary protocol.
When to choose MessagePack or Protobuf instead
Hand-rolled binary is fine for tightly-defined messages with few fields. Once your protocol grows — schema evolution, optional fields, nested structures — reach for MessagePack (drop-in JSON replacement, 30% smaller, no schema) or Protobuf (best compression, requires .proto definitions). We cover those in Track 5.