Files
2026-08-27 21:09:14 +00:00

35 lines
973 B
Plaintext

---
title: Enable compression for WebSocket messages
sidebarTitle: Enable compression
mode: center
---
Set the `perMessageDeflate` parameter to enable the [permessage-deflate](https://tools.ietf.org/html/rfc7692) WebSocket extension. Bun then negotiates compression with clients that support it. Messages sent with `ws.send()` are still uncompressed unless you opt in per message (see below).
```ts server.ts icon="/icons/typescript.svg"
Bun.serve({
// ...
websocket: {
// enable compression
perMessageDeflate: true,
},
});
```
---
To enable compression for individual messages, pass `true` as the second parameter to `ws.send()`. This requires `perMessageDeflate` to be enabled; otherwise Bun sends the message uncompressed.
```ts server.ts icon="/icons/typescript.svg"
Bun.serve({
// ...
websocket: {
perMessageDeflate: true,
async message(ws, message) {
// send a compressed message
ws.send(message, true);
},
},
});
```