Lightweight WebSocket library for Bun
- 🐰 Works with Bun
- ⚡ Zero dependencies
- ⚙️ Assigns an ID to each client connection
- 🟦 Written in TypeScript
bun a @jgtools/bunnywsimport { BunnyWS } from "@jgtools/bunnyws";
// ...import { BunnyMsg, BunnyWS, BunnyWSEvents, BunnyWSClient } from "@jgtools/bunnyws";
const events: BunnyWSEvents = {
open: (ws: BunnyWSClient) => {
console.log("Client has connected", ws.data.id);
},
message: (ws: BunnyWSClient, msg: BunnyMsg) => {
console.log("Received:", msg);
ws.send(msg); // send to client
ws.publish("global", msg); // send to all connected clients (excluding itself)
},
close: (ws: BunnyWSClient) => {
console.log("Client has disconnected:", ws.data.id);
}
}
const bws = new BunnyWS(8080, events);
setInterval(() => bws.publish("Published to all"), 3000);BunnyWS is a WebSocket server.
Constructor parameters:
| Parameter | Type |
|---|---|
port |
number |
events |
BunnyWSEvents |
Methods:
| Method | Type |
|---|---|
publish |
(msg: string | ArrayBufferView | ArrayBuffer, compress?: boolean) => number |
BunnyWSEvents is an interface that defines the event handlers for a BunnyWS server.
| Property | Type |
|---|---|
open |
(ws: BunnyWSClient) => void |
message |
(ws: BunnyWSClient, msg: BunnyMsg) => void |
close |
(ws: BunnyWSClient) => void |
MIT