blob: cec8c38025eb2a6fa14fa12b6ec8fb461649e964 (
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
 | ---
name: Upgrade an HTTP request to a WebSocket connection
---
Inside `fetch`, use the `server.upgrade()` function to upgrade an incoming `Request` to a WebSocket connection. Bun automatically returns a 101 Switching Protocols response if the upgrade succeeds.
Refer to the [WebSocket docs](/docs/api/websockets) for more information on building WebSocket servers.
```ts
const server = Bun.serve<{ authToken: string }>({
  fetch(req, server) {
    const success = server.upgrade(req);
    if (success) {
      // Bun automatically returns a 101 Switching Protocols
      // if the upgrade succeeds
      return undefined;
    }
    // handle HTTP request normally
    return new Response("Hello world!");
  },
  websocket: {
    // define websocket handlers
  },
});
console.log(`Listening on localhost:\${server.port}`);
```
 |