Skip to content

Commit 4150b2b

Browse files
docs: add example with Hono
Related: #1
1 parent acfe4ba commit 4150b2b

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Reference: https://socket.io/
1111

1212
<!-- TOC -->
1313
* [How to use](#how-to-use)
14+
* [With Bun's HTTP server](#with-buns-http-server)
15+
* [With Hono](#with-hono)
1416
* [Options](#options)
1517
* [`path`](#path)
1618
* [`pingTimeout`](#pingtimeout)
@@ -26,6 +28,8 @@ Reference: https://socket.io/
2628

2729
## How to use
2830

31+
### With Bun's HTTP server
32+
2933
```js
3034
import { Server as Engine } from "@socket.io/bun-engine";
3135
import { Server } from "socket.io";
@@ -38,17 +42,59 @@ const engine = new Engine({
3842

3943
io.bind(engine);
4044

41-
Bun.serve({
42-
...engine.handler(),
45+
io.on("connection", (socket) => {
46+
// ...
47+
});
48+
49+
export default {
4350
port: 3000,
4451
idleTimeout: 30, // must be greater than the "pingInterval" option of the engine, which defaults to 25 seconds
45-
});
52+
53+
...engine.handler(),
54+
};
55+
```
56+
57+
### With Hono
58+
59+
```js
60+
import { Server as Engine } from "@socket.io/bun-engine";
61+
import { Server } from "socket.io";
62+
import { Hono } from "hono";
63+
64+
const io = new Server();
65+
66+
const engine = new Engine();
67+
68+
io.bind(engine);
4669

4770
io.on("connection", (socket) => {
4871
// ...
4972
});
73+
74+
const app = new Hono();
75+
76+
const { websocket } = engine.handler();
77+
78+
export default {
79+
port: 3000,
80+
idleTimeout: 30, // must be greater than the "pingInterval" option of the engine, which defaults to 25 seconds
81+
82+
fetch(req, server) {
83+
const url = new URL(req.url);
84+
85+
if (url.pathname === "/socket.io/") {
86+
return engine.handleRequest(req, server);
87+
} else {
88+
return app.fetch(req, server);
89+
}
90+
},
91+
92+
websocket
93+
}
5094
```
5195

96+
Reference: https://hono.dev/docs/
97+
5298
## Options
5399

54100
### `path`

0 commit comments

Comments
 (0)