# Enable compression for WebSocket messages (/guides/websocket/compression)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 222 · updated: 2026-09-23 -->
Related: [Build a simple WebSocket server](/guides/websocket/simple.md), [Build a publish-subscribe WebSocket server](/guides/websocket/pubsub.md), [Set per-socket contextual data on a WebSocket](/guides/websocket/context.md), [Add a dependency](/guides/install/add.md)

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 icon="/icons/typescript.svg" title="server.ts"
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 icon="/icons/typescript.svg" title="server.ts"
Bun.serve({
  // ...
  websocket: {
    perMessageDeflate: true,
    async message(ws, message) {
      // send a compressed message
      ws.send(message, true);
    },
  },
});
```
