Silgi
Protocols

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:

PayloadJSONMessagePackSavings
1 object33 bytes22 bytes33%
10 objects661 bytes493 bytes25%
100 objects9.5 KB6.7 KB30%

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-msgpack on the request --> decodes the body as MessagePack
  • Accept: application/x-msgpack on 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:

TypeJSONMessagePack
String, Number, Boolean, nullYesYes
Object, ArrayYesYes
DateNo (becomes string)Yes (timestamp extension)
undefinedNo (dropped)Yes (distinct from null)
Set, Map, Error, RegExpNoYes (with moreTypes — enabled by default)
NaN, InfinityNo (becomes null)Yes (IEEE 754)
BigIntNo (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.

What's next?

  • devalue — rich type serialization for Date, Map, Set, BigInt, and circular refs
  • WebSocket — persistent connections for real-time and subscriptions
  • Client — the full client setup guide

On this page