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-league-server

v1.0.1

Published

A lightweight non-opinionated NPM library that works along side socket-league-client. This library contains a handler to receive messages from the hook on a websocket server which persists the state updates on a database and notifies clients of changes. B

Downloads

7

Readme

PRs Welcome License: MIT GitHub package.json version

socket-league-server: Websockets that transfer synced state

Table of Contents

Features

A lightweight non-opinionated NPM library that works along side socket-league-client. This library contains a handler to receive messages from the hook on a websocket server which persists the state updates on a database and notifies clients of changes. Both socket-league-server and socket-league-client should be used together.

Installation

npm install socket-league-server for access to the websocket server handler.

How It works

The synchronized hook server handler

The SyncHandler Class

A SyncHandler object should be instantiated with the database driver in which the state is to be tracked. If no database driver is specified, the default database driver will use JSON to track state.

Currently MongoDB and PostgresSQL are supported by pre-designed drivers. The TypeScript interface driver.ts provides the specifications for other drivers to implement.

Example 1: Instantiating the SyncHandler with MongoDB or Postgres as the database storing state.

const syncHandler = new SyncHandler(new MongoDriver(`myURI`)); const syncHandler = new SyncHandler(new PostgresDriver(`myURI`));

Example 2: Instantiating the SyncHandler with a JSON database.

const syncHandler = new SyncHandler(); is equivalent to const syncHandler = new SyncHandler(new JsonDriver());

Connecting to the database

The SyncHandler does not automatically connect to the database driver provided on its invocation.syncHandler.connect() should be used to initialize the asynchronous database connection. If the connected database driver is changed, syncHandler.connect() should be called again.

Attaching the SyncHandler to the Websocket Server such as ws

Finally, the SyncHandler object should be invoked during connection events for the websocket server implementation.

Below is the sample implementation for the ws library.

const syncHandler = new SyncHandler();
await syncHandler.connect();
const wsServer = new ws.Server({ noServer: true });
wsServer.on("connection", syncHandler.handleWsConnection);

Auto-Disconnect

By default, the SyncHandler automatically disconnects websockets that reach the close state while connected. This behavior can be changed by invoking the toggleAutoDisconnect() method.

const syncHandler = new SyncHandler();
console.log(syncHandler.toggleAutoDisconnect()); //returns and logs the current AutoDisconnect status.

State Merger

By default, the database preserves only the most recently received state. This behavior can be customized using the StateMerger class.

The properties of the merger can be accessed by calling .merger on the SyncHandler.

  1. setDefaultHandler can be used to set custom behavior for conflicting states, for example by diffing or calculating which state to keep.
const syncHandler = new SyncHandler();
syncHandler.merger.setDefaultHandler((serverState, oldState, newState) => Math.max( serverState + oldState, serverState + newState));
  1. registerHandler can be used to set custom behavior for conflicting states, but only for specific sessions, if the conflict management behavior is only desired in specific cases.
const syncHandler = new SyncHandler();
syncHandler.merger.registerHandler( '0', (serverState, oldState, newState) => Math.max( serverState + oldState, serverState + newState) );

Pre-processing State Changes

The SyncHandler can preprocess inputs received from clients for validity or encryption purposes.

A function can be set for this purpose by invoking setProcessState(), which takes as a parameter the helper function containing the logic to process the state.

This helper function should take one parameter representing an object containing keys of state, session, and action type, as well as the oldState if applicable, and return the processed object containing those same keys.

const syncHandler = new SyncHandler();
syncHandler.setProcessState( (stateUpdate) => {console.log(stateUpdate.session), return stateUpdate} );

The helper function set using setProcessState() can be cleared by invoking resetProcessState().

syncHandler.resetProcessState();

Server Setup

Now that your backend is setup it is time to setup your frontend. Please refer to the following NPM library for more information on how to set that up: socket-league-client.

Demo Apps

Echo Server

Check out our chat-app we built using our very own NPM libraries and hooks!

Our Echo Server is a chat application with four separate channels for conversation. Clients are synchronized to the server via websockets, which transmit updates relating to any messages sent or background changes. Feel free to cruise around and see how exactly it all comes together.

Contributing

We'd love for you to test this library out and submit any issues you encounter. Also feel free to fork to your own repo and submit pull requests!

Authors

Aaron Gaut
Kurt Crandall
Kyle Boudewyn
Trevor Mow
Zachary Lim