You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+10-9Lines changed: 10 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,14 @@
1
1
# Bun WebSocket Router
2
2
3
-
A type-safe WebSocket router for Bun with Zod-based message validation. Route WebSocket messages to handlers based on message type with full TypeScript support.
3
+
Tired of wrestling WebSocket connections like a tangled mess of holiday lights? `bun-ws-router` is here to bring order to the chaos! It's a type-safe WebSocket router for Bun, powered by Zod, making your real-time message handling smoother than a fresh jar of peanut butter. Route WebSocket messages to handlers based on message type with full TypeScript support.
4
4
5
5
### Key Features
6
6
7
-
-**Type-safe messaging**: Built-in validation using Zod schemas
8
-
-**Simple API**: Intuitive routing system for WebSocket messages
- 🧩 **Flexible**: Works with any Bun server setup, including Hono
11
+
- 🪶 **Lightweight**: Minimal dependencies for fast startup
13
12
14
13
Perfect for real-time applications like chat systems, live dashboards, multiplayer games, and notification services.
15
14
@@ -105,6 +104,8 @@ Bun.serve({
105
104
106
105
The `verifyIdToken` function is a placeholder for your authentication logic which could use user ID token verification from `firebase-admin` or any other authentication library.
107
106
107
+
By verifying the user _before_ the WebSocket connection is fully established and passing the `user` data, you ensure that only authenticated users can connect, and you have their info ready to use in your `onOpen`, `onMessage`, and `onClose` handlers without needing to ask again. Secure _and_ convenient!
108
+
108
109
## How to define message types
109
110
110
111
You can define message types using the `messageSchema` function from `bun-ws-router`. This function takes a message type name such as `JOIN_ROOM`, `SEND_MESSAGE` etc. and a Zod schema for the message payload. The following example demonstrates how to define message types for a chat application.
@@ -180,7 +181,7 @@ ws.onClose((c) => {
180
181
});
181
182
```
182
183
183
-
**Note:** The `c.send(...)` function sends a message back to the _current_client.
184
+
**Important Note:** The `c.send(...)` function sends a message back _only_to the client that sent the original message. For broadcasting to rooms or multiple clients, you'll need to implement your own logic (we handle the routing, you handle the party!).
184
185
185
186
## How to compose routes
186
187
@@ -201,7 +202,7 @@ Where `chatRoutes` and `notificationRoutes` are other router instances defined i
201
202
202
203
## Support
203
204
204
-
Feel free to discuss any issues or suggestions on our [Discord](https://discord.com/invite/bSsv7XM) channel. We welcome contributions and feedback from the community.
205
+
Got questions? Hit a snag? Or just want to share your awesome WebSocket creation? Find us on [Discord](https://discord.com/invite/bSsv7XM). We promise we don't bite (usually 😉).
0 commit comments