Skip to content

bit-js/bun-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bun-utils

An utilities library for Bun.

WebSocket router

A WebSocket router for Bun.

import { ws } from '@bit-js/bun-utils';

To create a route.

const route = ws.route<{ id: string }>({
    message(ws) {
        ws.send(ws.data.id);
    }
})

Use in a request handler.

function fetch(req: Request) {
    return route.upgrade(req, { 
        data: { id: performance.now() } 
    });
}

Serve the fetch handler (this step creates the server and bind all routes).

const server = ws.serve({
    // Bun.serve options
    server: { fetch },

    // Bun WebSocket options
    ws: { publishToSelf: true }
});

Or with an already created server instance without ws.serve.

ws.bind(server);

Releases

No releases published

Packages

No packages published