Files
bun.sh/docs/guides/websocket/compression.mdx
2025-11-05 11:14:21 -08:00

34 lines
769 B
Plaintext

---
title: Enable compression for WebSocket messages
sidebarTitle: Enable compression
mode: center
---
Per-message compression can be enabled with the `perMessageDeflate` parameter. When set, all messages will be compressed using the [permessage-deflate](https://tools.ietf.org/html/rfc7692) WebSocket extension.
```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()`.
```ts server.ts icon="/icons/typescript.svg"
Bun.serve({
// ...
websocket: {
async message(ws, message) {
// send a compressed message
ws.send(message, true);
},
},
});
```