mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
34 lines
769 B
Plaintext
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);
|
|
},
|
|
},
|
|
});
|
|
```
|