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

wspackage

v0.5.8

Published

WebSocket Package

Downloads

21

Readme

WebSocket Package

For user

Install package

npm i wspackage

How to use

  1. Write script file
    Server Example (NodeJS):

    // server.js
    const { wsServer } = require('wspackage');
    
    /*
        wsServer config structure:
        {
            httpServer: @undefined | @http.Server
            port: @undefined | @number | @string,
            host: @number | @string,
            acceptedProtocol: @string | [ @string, ... ]
        }
    
        Note: If acceptedProtocol is undefined, you should use protocol function to set sub protocol
    */
    const ws = wsServer({
        httpServer: http.createServer((req, res) => {
            res.writeHead(404);
            res.end();
        }),
        port: 1234,
        host: '0.0.0.0',
        acceptedProtocol: ['protocol1', 'protocol2']
    });
    ws.listen();
    
    const callback = (e, ...args) => {
        console.log(e.type);        // event name
        console.log(e.sender);      // sender info (socket connection)
        console.log(e.timestamp);   // new Date()
        console.log(args);
    };
    
    // event handler for all protocols
    ws.on('register_event_name', callback);                               // register event
    ws.once('register_once_event_name', callback);                        // register event once
    ws.off('remove_event_name', callback);                                // remove event
    ws.emit('trigger_event_name', [arg1], [arg2], [...]);                 // trigger event (broadcast)
    ws.send('target_id', 'trigger_event_name', [arg1], [arg2], [...]);    // trigger event (target socket connection)
    
    // wspackage default event
    ws.on('listen', callback);      // server listen
    ws.on('open', callback);        // websocket connection open
    ws.on('close', callback);       // websocket connection closed
    ws.on('error', callback);       // websocket connection throw error
    
    // event handler for sub protocol
    const conn1 = ws.protocol('protocol1');
    conn1.on('trigger_event_name', callback);
    const conn2 = ws.protocol('protocol2');
    conn2.on('trigger_event_name', callback);
    
    // other definition in wspackage
    ws.close();                     // close all websocket connection

    Client Example (JavaScript ES6):

    // client.js
    import { WSPack } from './browser-client.esm.js';
    
    let callback = (e, ...args) => {
        console.log(e);
        console.log(args);
    };
    
    let conn1 = WSPack('ws://127.0.0.1:1234', 'protocol1');
    
    // event handler
    conn1.on('register_event_name', callback);                  // register event
    conn1.once('register_once_event_name', callback);           // register event once
    conn1.off('remove_event_name', callback);                   // remove event
    conn1.emit('trigger_event_name', [arg1], [arg2], [...]);    // trigger event
    
    // wspackage default event
    conn1.on('open', callback);         // websocket connection open
    conn1.on('close', callback);        // websocket connection closed
    conn1.on('error', callback);        // websocket connection throw error
    
    // other definition in wspackage
    conn1.close();                      // close websocket connection between protocol1 and server
    
    // event handler for another sub protocol
    let conn2 = WSPack('ws://127.0.0.1:1234', 'protocol2');
    conn2.on('trigger_event_name', callback);
    • acceptedProtocol can only use lowercase in wsServer config.
    • Please refer to Event Emitter for other event handler usages.
    • Callback parameter is based on ws.
  2. Run command line:

    node server.js

  3. Run client page


For maintainer

Install project

  • Clone project:

    git clone <project-url>

  • Install dependency package:

    npm install --production

Build and Run

  • Run test basic server (use node):

    node ./test/basic/server.js

  • Run test basic server (use npm):

    npm run test_basic

  • Run test multiple protocol server (use node):

    node ./test/multiple/server.js

  • Run test multiple protocol server (use npm):

    npm run test_multiple

  • Run test shutdown server (use node):

    node ./test/shutdown/server.js

  • Run test shutdown server (use npm):

    npm run test_shutdown