MessagePack
Binary protocol for smaller payloads and faster encoding. One flag on the client, zero changes on the server.
MessagePack is a binary serialization format. It encodes the same data as JSON, but in fewer bytes. Think of it as "JSON, but binary." The result is smaller payloads and faster parsing.
Why use it?
JSON is text. Every number, every key name, every bracket is a character that takes up space. MessagePack uses a compact binary representation instead:
| Payload | JSON | MessagePack | Savings |
|---|---|---|---|
| 1 object | 33 bytes | 22 bytes | 33% |
| 10 objects | 661 bytes | 493 bytes | 25% |
| 100 objects | 9.5 KB | 6.7 KB | 30% |
This matters most for mobile apps, APIs with large responses, and high-traffic services where bandwidth adds up.
Client setup
Enable binary mode with one flag:
import { } from 'silgi/client/ofetch'
import { } from 'silgi/client'
const = ({
: 'http://localhost:3000',
: true,
})
const = <>()
// Use exactly like before — the encoding is transparent
const = await .users.list({ : 10 })That's it. The client now encodes requests as MessagePack and sets the Accept: application/x-msgpack header. The server detects this and responds in MessagePack.
Your application code doesn't change at all. You still pass and receive regular JavaScript objects. The binary encoding/decoding happens inside the link.
Server behavior
The server handles MessagePack automatically. It checks two headers:
Content-Type: application/x-msgpackon the request --> decodes the body as MessagePackAccept: application/x-msgpackon the request --> encodes the response as MessagePack
No server configuration is needed. Both serve() and handler() support this out of the box.
Direct API
If you need to encode/decode MessagePack manually (for example, in a custom transport), Silgi exports the codec:
import { , } from 'silgi/msgpack'
// Encode to binary
const = ({ : 'Alice', : new () })
// buf is an ArrayBuffer — ready to use as a Response body
// Decode from binary
const = (new ())
// data.joined is a Date object (preserved natively)Supported types
MessagePack natively supports more types than JSON:
| Type | JSON | MessagePack |
|---|---|---|
| String, Number, Boolean, null | Yes | Yes |
| Object, Array | Yes | Yes |
| Date | No (becomes string) | Yes (timestamp extension) |
| undefined | No (dropped) | Yes (distinct from null) |
| Set, Map, Error, RegExp | No | Yes (with moreTypes — enabled by default) |
| NaN, Infinity | No (becomes null) | Yes (IEEE 754) |
| BigInt | No (throws) | 64-bit integers |
If you need BigInt beyond 64-bit, or need circular references, use devalue instead.
WebSocket + MessagePack
You can also use MessagePack over WebSocket for even lower overhead:
import { } from 'silgi/ws'
(server, appRouter, { : true })Messages are sent as binary WebSocket frames instead of text.