aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/websocket/compression.md
blob: e98d8f0c983f88168c0eb92df7645d756e33ca68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
---
name: Enable compression for WebSocket messages
---

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
Bun.serve({
  // ...
  websocket: {
    // enable compression
    perMessageDeflate: true,
  },
});
```

---

To enable compression for individual messages, pass `true` as the second parameter to `ws.send()`.

```ts
Bun.serve({
  // ...
  websocket: {
    async message(ws, message) {
      // send a compressed message
      ws.send("Hello world!", true);
    },
  },
});
```