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

socket-sorcerer

v6.2.1

Published

Node.js Websocket Framework

Downloads

604

Readme

npm i -s socket-sorcerer;

Socket Sorcerer is a light-weight WebSocket framework based on 'ws' package.

In comparison with other libraries it has next advantages:

  • already implemented reconnects, pinging, authentication etc
  • the simplest implementation of full functionality in less rows of Your code
  • easily integration in any application

Build server:

import { WebSocketServer } from 'socket-sorcerer/server';
import { createServer } from 'http';

const server = createServer().listen(8088);

const wss = new WebSocketServer({
  serverOptions: {
    server
  },
  pingInterval: 5000,
  pingTimeout: 15000,
  authenticate: {
    eventName: 'auth',
    async eventHandler(token) {
      // token may be simple string or object contains string "token" property and other parameters
      const user = { _id: '5e1c62a969a07513e8f99a73' };
      return user._id;
    },
    authTimeout: 3000
  },
  events: {
    connect (connectionId, userId, token, userIp) {
      // event is emitting once after successful authentication
      wss.getManager().join({
        channel: 'my awesome channel',
        user: uid
      });
      wss.getManager().join({
        channel: 'for MacOS users only',
        connection: connectionId
      });
    },
    flud (data, connectionId, userId) {
      //process the data
    },
    disconnect (connectionId, userId, token) {
      // event is emitting when socket is closed
    },
    afterConnect (connectionId, userId, token) {
      // event is emitting  right after connect event and at each successful ping-pong
    }
  }
});

// Built server has its own manager property which You can export end use anywhere in the code:

const manager = wss.getManager();
manager.send({
  channel: 'for MacOS users only',
  event: 'update',
  data: {
    version: '1.2.3',
    downloadUrl: '...'
  }
});



Server Options:
  • serverOptions: see ws.ServerOptions
  • pingInterval: interval between to ping probes in milliseconds,
  • pingTimeout: value when connection will be closed after ping probe fails in milliseconds,
  • authenticate.eventName: Your custom authenticate event name, should be implemented on client side too,
  • authenticate.eventHandler: Your custom authenticate event handler which should take provided from client side token and return user id
  • authenticate.authTimeout: value when connection will be closed after auth probe fails in milliseconds
  • events: Server side event handlers, all of them besides 'connect' and 'disconnect' take two args (data and uid, "connect" and "disconnect" events take one arg - uid). You can use '*' to define middleware which will be called previously (except "connect", "disconnect" and service events)
Build client at the front end side:
import { WebSocketClient } from 'socket-sorcerer/client';

const ws = new WebSocketClient({
  serverUrl: 'ws://localhost:8088',
  token: 'clientPersonalConnectToken',
  doReconnectOnClose: true,
  reconnectInterval: 5000,
  authEventName: 'auth',
  events: {
    test: (data) => {
      ws.emit({
        channel: 'test',
        event: 'message',
        data: {
          message: 'Hi there'
        }
      })
    }
  }
});

// You can export ws object and use it anywhere, e.g. just send something to the server 
 ws.emit({
   channel: 'flud',
   event: 'message',
   data: {
     message: 'Hi there'
   }
 });
Client Options:
  • serverUrl: Previously created WebSocket Server URL, f.e. 'ws://localhost:8088',
  • token: Personal token for authentication
  • doReconnectOnClose: Boolean identifier which indicate to do reconnects, recommended value is true,
  • reconnectInterval: Interval between reconnect attempts in milliseconds,
  • authEventName: Authenticate event name the same as the server side,
  • events: Client side event handlers