
How I Fine-Tuned Supabase Realtime Communication to the Max
This page has been translated by machine translation. View original
Introduction
At the Supabase booth at AWS re:Invent 2026, I learned that Realtime Broadcast now supports binary payloads. The conversation turned to online multiplayer games, and based on my experience working in the game industry, I mentioned that carefully tuning payloads down to the byte layout is a common practice in the field.
By the way, the rental Wi-Fi at my business trip destination happened to be slow and was giving me trouble, but then it occurred to me that it might actually be a perfect environment for trying out a "laggy" online multiplayer setup... Striking while the iron was hot, I decided to run a payload optimization experiment to see whether things could run smoothly even on a weak connection.
Incidentally, I have previously built a competitive game using Supabase Realtime Broadcast.
I didn't notice any lag in that game, but what about a game in which a large number of objects appear? This time's subject is a game in which a large number of objects move in a swarm. I compared the behavior of two patterns: one where the synchronized data is straightforwardly built as JSON, and one where coordinates are packed into binary at 10 bits each.
As a result, the number of objects at which the game could be played comfortably changed significantly. In my test environment, the JSON version started to stutter at 20 objects, whereas the binary version could be increased up to 250 objects.

Top: 40 objects with JSON, bottom: 250 objects with binary
What are Realtime Broadcast binary payloads?
Broadcast is a Supabase Realtime feature that lets clients connected to the same channel send messages to each other. With the June 2026 update, supabase-js 2.91.0 and later can send binary instead of JSON as the payload.
Test environment
- supabase-js 2.120.0
- Supabase (us-west-1)
- Google Chrome 158
Target audience
- Those building apps that synchronize state in real time with Supabase Realtime
- Those who want their apps to run comfortably even for users with weak connections
References
- Realtime Broadcast now supports binary payloads (Supabase changelog)
- Broadcast (Supabase official documentation)
- Reading and Writing Packets (Gaffer On Games)
- Snapshot Compression (Gaffer On Games)
Background
In real-time games, you need to keep sending the positions of all objects dozens of times per second. The amount sent grows multiplicatively, as the product of factors such as the number of objects, the size per object, and the synchronization frequency. Therefore, on a weak connection, the next message piles up before the previous one has fully arrived, and the on-screen movement falls further and further behind. This is "lag."
In online game development, many techniques have been accumulated to address this problem by making the data being sent itself smaller. In Glenn Fiedler's blog Gaffer On Games, he explains that if you know the range of a value, you can represent it with only the necessary number of bits, as follows.
What if we have a value in the range [0,1000] we really only need 10 bits to represent all possible values.
(Translation) A value in the range 0 to 1000 can have all of its possible values represented with 10 bits.
The three representative techniques are as follows.
- Quantization: round fractional numbers such as coordinates to integers of the required precision
- Bit packing: determine the required number of bits from the range of values and pack them without regard for byte boundaries
- Delta compression: send only what has changed since last time
In this article, "packing tightly" refers to "quantization" and "bit packing" among these.
Until now, Supabase Realtime Broadcast could only send payloads as JSON. Even if you packed data at the bit level, you had to convert it into something like a string in order to send it. With support for binary payloads, you can now send the packed byte sequence as is.

Test method
N objects are placed in an 800 × 600 screen area. The objects consist of the first 2 as players and the rest as NPCs. The players move in circles, and the NPCs swarm toward the nearest player. On screen, every object is drawn as a dot, and players and NPCs are distinguished by color. The sender transmits the coordinates of all objects at 20Hz for 30 seconds.

On the receiving tab, I open the page, connect to the channel, and then apply a bandwidth throttle equivalent to DevTools' 3G (400 kbps). The criterion for "no lag and comfortable to play" is "the delay does not grow over time and the receive delay p95 is 250ms or less." I measured the maximum number of objects at which it was comfortable to play and the minimum number of objects at which it was not, three times each.
For the measurements, I used the following two formats.
JSON version: communicates using straightforwardly built JSONBinary version: communicates using binary with coordinates packed at the bit level
How the payloads are packed
In the JSON version, each object has an ID, coordinates, and a type that distinguishes players from NPCs. The coordinates are the values taken from a Float32Array as is. The decimal digits and key names add up, making it about 67B per object.
{"seq":5,"t":1796170000000,"units":[{"id":0,"x":387.5765686035156,"y":332.2253112792969,"team":"player"},{"id":1,"x":704.95849609375,"y":253.72549438476562,"team":"player"},{"id":2,"x":144.22462463378906,"y":317.1701965332031,"team":"npc"}]}
On the other hand, the binary version was trimmed down as follows.
- Screen pixel units are enough for coordinates, so they are rounded to integers
- x is 0 to 800
- y is 0 to 600
- both fit within 10 bits (0 to 1023)
- Each object is represented by 20 bits of x and y, packed without worrying about byte boundaries
- 5B for 2 objects
- ID and type are not synchronized
- The ID is represented by the order, with the convention that the first 2 objects are players
- The first 8B hold the sequence number (2B), send time (4B), and number of objects (2B)
The size of one binary-version message is 8 + ceil(N × 20 / 8) bytes. Compared with the JSON version, it looks like this.
| Number of objects | JSON version (B) | Binary version (B) |
|---|---|---|
| 100 | 6,732 | 258 |
| 250 | 16,919 | 633 |
At 250 objects, one binary-version message is about 4% the size of the JSON version. With each additional object, the JSON version grows by about 67B and the binary version by 2.5B. Therefore, the more objects there are, the more bytes you can save by packing.
The part that writes the coordinates simply packs bit by bit from the most significant bit.
const bytes = new Uint8Array(buf, 8) // after the 8B header
let bitPos = 0
const write = (v: number) => {
for (let b = 9; b >= 0; b--) {
if ((v >> b) & 1) bytes[bitPos >> 3] |= 0x80 >> (bitPos & 7)
bitPos++
}
}
for (let i = 0; i < n; i++) {
write(Math.round(x[i]))
write(Math.round(y[i]))
}
For the send time, only the lower 32 bits of Date.now() are sent. The receiving side restores the original value from the difference with the receive time.
const t = recvT - (((recvT >>> 0) - t32) >>> 0)
Full encode and decode
const BP_HEADER_BYTES = 8
const COORD_BITS = 10
export function packedBytes(n: number): number {
return BP_HEADER_BYTES + Math.ceil((n * COORD_BITS * 2) / 8)
}
function encodePacked(seq: number, t: number, u: Units): ArrayBuffer {
const n = u.x.length
const buf = new ArrayBuffer(packedBytes(n))
const view = new DataView(buf)
view.setUint16(0, seq & 0xffff)
view.setUint32(2, t >>> 0)
view.setUint16(6, n)
const bytes = new Uint8Array(buf, BP_HEADER_BYTES)
let bitPos = 0
const write = (v: number) => {
for (let b = COORD_BITS - 1; b >= 0; b--) {
if ((v >> b) & 1) bytes[bitPos >> 3] |= 0x80 >> (bitPos & 7)
bitPos++
}
}
for (let i = 0; i < n; i++) {
write(Math.round(u.x[i]))
write(Math.round(u.y[i]))
}
return buf
}
export function decodePacked(buf: ArrayBuffer, recvT: number): { seq: number; t: number; xs: number[]; ys: number[] } {
const view = new DataView(buf)
const seq = view.getUint16(0)
const t32 = view.getUint32(2)
const n = view.getUint16(6)
const bytes = new Uint8Array(buf, BP_HEADER_BYTES)
let bitPos = 0
const read = () => {
let v = 0
for (let b = 0; b < COORD_BITS; b++) {
v = (v << 1) | ((bytes[bitPos >> 3] >> (7 - (bitPos & 7))) & 1)
bitPos++
}
return v
}
const xs = new Array(n)
const ys = new Array(n)
for (let i = 0; i < n; i++) {
xs[i] = read()
ys[i] = read()
}
// 送信時刻は下位 32 ビットしか送らないため、受信時刻から完全な値を復元する。
const t = recvT - (((recvT >>> 0) - t32) >>> 0)
return { seq, t, xs, ys }
}
Sending and receiving are written the same way as normal Broadcast. An ArrayBuffer arrives as the payload on the receiving side.
// Sender
channel.send({ type: 'broadcast', event: 's', payload: encodePacked(seq, Date.now(), units) })
// Receiver
channel.on('broadcast', { event: 's' }, (msg) => {
const d = decodePacked(msg.payload as ArrayBuffer, Date.now())
// Draw d.xs and d.ys
})
Test results
The maximum number of objects at which play was comfortable in all 3 trials for each format is as follows.
| Format | Max objects played comfortably | Size of 1 message (B) | Max p95 (ms) |
|---|---|---|---|
| JSON version | 10 | 704 | 176 |
| Binary version | 250 | 633 | 178 |
The max p95 column lists the largest p95 value among the 3 trials. The size of one JSON-version message varies slightly each time depending on the number of decimal digits in the coordinates, so this column lists the average of the 3 trials.

With the JSON version, play was comfortable in all 3 trials with 10 objects, but with 20 objects the p95 exceeded the 250ms guideline in all 3 trials. With 30 objects, the p50 exceeded 4 seconds. The binary version was comfortable in all 3 trials even with 250 objects, so it could synchronize 25 times as many objects over the same connection.
The size of one message when play was not comfortable was 1,376B for the JSON version with 20 objects and 1,883B for the binary version with 750 objects.
Caveat: pass an ArrayBuffer, not a Uint8Array
If you pass the packed buffer to send() still wrapped in a Uint8Array, it is sent as JSON instead of binary. When I tried it with 100 objects' worth of binary, one frame ballooned from 464B to 3,563B. It was converted to JSON keyed by the index of each byte, and it arrived in that form on the receiving side as well.
In supabase-js 2.120.0, the payload is checked with instanceof ArrayBuffer, and only those that match are sent as binary. The changelog also lists views such as Uint8Array as supported. However, when passing to channel.send(), it is safest to make it an ArrayBuffer.
If you assemble the data with a Uint8Array, slice out only the range that the view points to and pass that.
// Even if u8 points to part of a larger buffer, send only that range
const payload = u8.buffer.slice(u8.byteOffset, u8.byteOffset + u8.byteLength)
channel.send({ type: 'broadcast', event: 's', payload })
To check this in your own code, select the WebSocket connection in the DevTools Network tab and look at the size of the frames shown in Messages.
Discussion
From these results, it appears that the size of one message in real-time communication greatly affects the game experience. It seems easier to design if you decide in advance how many bytes can be used per message, based on the expected connection bandwidth and synchronization frequency.
However, even for the JSON version with 20 objects, where play was not comfortable, sending 1,376B at 20Hz is about 28KB/s, which does not reach the 3G limit of 50KB/s. I did not dig into the reason, but I suspect that WebSocket and TCP overhead and the way the bandwidth throttling is applied have an influence. It would be safe to estimate the budget with some margin rather than using up the full bandwidth.
Summary
If you build an app that synchronizes state with Supabase Realtime Broadcast, I recommend reproducing a "laggy" environment and checking it. If lag appears, simply rounding coordinates to integers and packing them at the bit level may greatly increase the number of objects you can synchronize. When sending the packed binary, pass an ArrayBuffer, not a Uint8Array. I hope this article is helpful to those building real-time apps with Supabase.