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 🙏

© 2024 – Pkg Stats / Ryan Hefner

snub-ws

v3.1.4

Published

WebSocket server middleware for snub

Downloads

68

Readme

Snub-WS

Middleware WS server that allows you to run web-sockets over snub.

Usage

npm install snub

npm install snub-ws

Basic Example

With redis installed and running with default port and no auth.

const Snub = require('snub');
const snub = new Snub();
const SnubWS = require('snub-ws');

const snubws = new SnubWS(
  debug: true
);

snub.use(snubws);

Config options

{
  port: 8585, // Web-socket server port
  debug: true, // Bool, turns on verbose messaging.
  mutliLogin: true, // Bool, can the same username connection more than once?
  auth: 'auth-event', // String OR Function OR bool for no auth required
  authTimeout: 3000, // how long to wait for the client to auth before disconnecting
  throttle: [50, 5000], // X number of messages per Y milliseconds before disconnecting
  idleTimeout: 1000 * 60 * 60, // how long can an idle client be connected
}

Auth Config extra

auth can be passed 1 of 3 things

Function

A function passed to auth

{
  auth: function (auth, accept) {
    if (auth.username == 'username')
      return accept(true); // run accept to authenticate the web-socket client connection.
    accept(false); // run with false to decline the web-socket connection and disconnect the client.
  }
}

Snub event

{
  auth: 'authenticate-client';
}
snub.on('ws:authenticate-client', function (auth, accept) {
  // console.log(auth);
  if (auth.username == 'username') return accept(true);
  reply(false);
});

TODO docs

False for no auth

Authenticating a client

The first thing a client should do after connecting is send an authenitication message. username is required, the entire object will be passed to your authentication method.

['_auth', { username, password}];

Sending messages from the client

Web socket message objects should be JSON stringified arrays. The first item is always the event name followed by payload, followed by the replyId. Only event name is required.

['event-name', { payload }, replyId];

String

A string passed to auth will run the method as a snub event. Reply true or false.

{
  auth: 'authenticate-client';
}

snub.on('ws:authenticate-client', function (auth, reply) {
  console.log(auth);
  if (auth.username == 'username') return reply(true);
  reply(false);
});

Boolean

A bool with false will authenticate any web-socket client connection.

{
  auth: false;
}

Events

Client > Server

When a client sends data to the server the event will be prefixed with ws:

{
  from: { // client socket info
    id: '89668-mnm4i6', // unique uuid generated by snub-ws
    username: 'username', // defined in auth
    channels: [], // clients channel list
    connected: true, // connection status
    authenticated: true, // auth status
    connectTime: 1583803891546, // timestamp
    remoteAddress: '::1', // clients ip address
    meta: {} // client meta obj
  },
  payload: {}, // payload from client
  _ts: 1583803922264 // timestamp
}

Simple snub listener. inbound messages from client will be prefixed with ws:

snub.on('ws:do-math', function (event, reply) {
  console.log('domath');
  reply(event.payload * 10);
});

Server > Client

Send event to all clients

snub.poly('ws:send-all', ['event-name', { payload }]).send();

Send event to channel

snub.poly('ws:send-channel:' + 'channel6', ['event-name', { payload }]).send();

Send event to multiple channels

snub
  .poly('ws:send-channel:' + ['channel6', 'channel2', 'channel3'].join(','), [
    'event-name',
    { payload },
  ])
  .send();

Send event to client

snub.poly('ws:send:' + 'username', ['event-name', { payload }]).send();
// or
snub.poly('ws:send:' + event.from.id, ['event-name', { payload }]).send();

Send event to multiple clients

snub
  .poly('ws:send-some, [
    'event-name',
    ['user1', 'user2', 'user3']
    { payload },
  ])
  .send();
// use this only for a small amount of users.
snub
  .poly('ws:send:' + ['user1', 'user2', 'user3'].join(','), [
    'event-name',
    { payload },
  ])
  .send();

Channels

snub.poly('ws:add-channel:' + 'username', ['channel1', 'channel2']).send(); // add to existing channels
snub.poly('ws:set-channel:' + 'username', ['channel1', 'channel2']).send(); // wipes and sets channels
snub.poly('ws:del-channel:' + 'username', ['channel1', 'channel2']).send(); // removes channels

// send to channel
snub.poly('ws:send-channel:' + 'channel6', ['event-name', { payload }]).send();

Meta

You can set meta against a client, which will be availbe in the event.from.meta object

snub.poly('ws:set-meta:' + 'username', { prop1: 'AOK' }).send();
snub
  .mono('ws:get-clients:' + 'usernameOrId')
  .replyAt((clients) => {
    // clients = Array of client states Inludes client.meta
  })
  .send();

WS Client emitted events

ws:connected-clients-update will be poly emitted when client connects or client state changes. ws:connected-clients-update mono emitted as above

ws:connected-clients-offline will be poly emitted with client state as payload when client disconects ws:connected-clients-offline-mono mono emitted event as above