--- 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}`); ``` lue='add-license'>add-license Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/fetch.test.js (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2022-06-22Fix bug with exceptions inside EventTargetGravatar Jarred Sumner 11-137/+413
2022-06-22Add a way to schedule microtasks from C++Gravatar Jarred Sumner 5-35/+77
2022-06-22Update javascript.zigGravatar Jarred Sumner 1-13/+152
2022-06-22Clean up perf hooks a littleGravatar Jarred Sumner 1-9/+21
2022-06-22use JSValue for `bun test`Gravatar Jarred Sumner 1-13/+19