npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

bifrost-wss

v5.0.1

Published

WSS Manager

Readme

bifrost-wss

NPM VersionRelease Date

WebSocket Server & Client library for simple authorized publisher/subscriber communication with rooms/topics.
Also provides a simple request/response pattern (via command).

Provides full control over handlers and instances, with additional helpers for easy integration with WebSocket Server (WSS).

Installation

npm i bifrost-wss

Usage

Server side

import {WSSManager} from 'bifrost-wss'
 const server = new WSSManager( {
    port: 8095,
    host: "localhost",
});
server.createRoom('room-1')
server.createRoom('room-2')

server.addHandler('authorize', function open(peer, accessToken) {
    if(accessToken === process.env.ACCESS_TOKEN) {
        // Deal with Auth for instance, and modify the peer object
        peer.isAuth = true;
    }
})

// Allow a peer to send a command "increment" and get a response
server.addHandler('increment', function increment(peer, message) {
    if(message.requestId){
        peer.send({value: i++, requestId: message.requestId})
    }
});

await server.start()
setInterval(()=>{
    server.logger.level = 'trace';
    server.broadcastRoom('room-1', {type: 'hello', payload: 'room1'})
    server.broadcastRoom('room-2', {type: 'hello', payload: 'room2'})
    server.broadcastAll({type: 'hello', payload: 'all'})
},1000)


Client side

import {WSClient} from 'bifrost-wss'

const client = new WSClient({
    port: 8095,
    host: "localhost",
    headers: {
        access_token: process.env.ACCESS_TOKEN,
    }
});

client.addHandler('open', () => {
    client.authorize();
    setTimeout(() => client.subscribe('room-1'), 10);
    setTimeout(() => client.subscribe('room-2'), 5000);
});

// global handler for all messages, they are in JSON already
client.addHandler('message', (message) => {
    console.log(message);
});

// Will be called only for messages from room-1
client.addHandler('room-1', function message(message) {
    console.log('room-1 handler', message)
});

await client.open();

// Will send a command to the server and wait for a response
const req = client.send({
    cmd: 'increment',
})
const res = await req;
console.log('increment response', res)

Commands

When .send() is called with a cmd property, a requestId will be generated, and a listener associated with the requestId will be created. The response will be awaited and the listener removed.

{
    cmd: 'subscribe',
    room: 'time-beacon'
}

Events

{
    topic: 'time-beacon',
    payload: {time: 1234567890}
}

Features

  • Room/Topic-Based Communication: Enables communication in specific channels.
  • WebSocket Server and Client: Provides both server and client components.
  • Reconnection: Reconnects automatically if the connection drops.
  • Authorization: Pre-build authorization logic
  • On-the-fly room management: Allows for the creation and removal of rooms on the fly.
  • Extensible Event Handlers: Supports adding custom handlers for WebSocket events.
  • Request/Response Pattern: Supports a simple request/response pattern.

API Reference

WSSManager (Server Component)

new WSSManager(options)

  • Description: Initializes a new WebSocket server.
  • Parameters:
    • options: Object containing configuration options.
      • port: (Number) The port number for the server.
      • host: (String) The host address for the server.
      • logger: (Object) Custom logger object.

start()

  • Description: Starts the WebSocket server.

stop()

  • Description: Stops the WebSocket server.

createRoom(roomName, force = false)

  • Description: Creates a new room.
  • Parameters:
    • roomName: (String) Name of the room to create.
    • force: (Boolean) Whether to force the creation of the room if it already exists.

removeRoom(roomName)

  • Description: Removes an existing room.
  • Parameters:
    • roomName: (String) Name of the room to remove.

broadcastRoom(roomName, message, sender = null, broadcastToSelf = false)

  • Description: Broadcasts a message to all clients in a specified room.
  • Parameters:
    • roomName: (String) Name of the room.
    • message: (Object) Message object to broadcast.
    • sender: (Object) Sender peer object. (Optional)
    • broadcastToSelf: (Boolean) Whether to broadcast to the sender. (Optional)

broadcastAll(message)

  • Description: Broadcasts a message to all connected clients.
  • Parameters:
    • message: (Object) Message object to broadcast.

addHandler(type, handler)

  • Description: Adds a handler for a specific message type.
  • Parameters:
    • type: (String) Type of message.
    • handler: (Function) Handler function.

WSClient (Client Component)

new WSClient(options)

  • Description: Initializes a new WebSocket client.
  • Parameters:
    • options: Object containing configuration options.
      • port: (Number) The port number of the server.
      • host: (String) The host address of the server.
      • headers: (Object) Headers for WebSocket connection.
      • isSecure: (Boolean) When true, use wss - defaults to false.

open()

  • Description: Opens the WebSocket connection.

close()

  • Description: Closes the WebSocket connection.

subscribe(room)

  • Description: Subscribes to a specific room.
  • Parameters:
    • room: (String) Name of the room to subscribe to.

authorize(accessToken)

  • Description: Authorizes the client using an access token.
  • Parameters:
    • accessToken: (String) Access token for authorization.

send(message)

  • Description: Sends a message through the WebSocket.
  • Parameters:
    • message: (Object/String) Message object or string to send.

addHandler(name, handler)

  • Description: Adds a handler for specific client events.
  • Parameters:
    • name: (String) Name of the event.
    • handler: (Function) Handler function.