4.0.8 • Published 2 years ago

@jgtools/bunnyws v4.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

BunnyWS

npm npm GitHub

Lightweight WebSocket library for Bun

Features

  • :rabbit: Works with Bun
  • :zap: Zero dependencies
  • :gear: Assigns an ID to each client connection
  • :blue_square: Written in TypeScript

Installation

Using bun

bun a @jgtools/bunnyws
import { BunnyWS } from "@jgtools/bunnyws";
// ...

Usage

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);

Docs

BunnyWS

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

BunnyWSEvents is an interface that defines the event handlers for a BunnyWS server.

PropertyType
open(ws: BunnyWSClient) => void
message(ws: BunnyWSClient, msg: BunnyMsg) => void
close(ws: BunnyWSClient) => void

License

MIT

4.0.8

2 years ago

4.0.7

2 years ago

4.0.6

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago