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

client-ketchup

v1.0.0

Published

A simple interface for keeping remote clients up to date with their authoritative state

Downloads

3

Readme

client-ketchup npm version Build Status

A simple interface for keeping remote clients up to date with their authoritative state

Initial Motivation

The goal of client-ketchup is to be a small API for managing the states of a constantly changing set of connected clients.

A server might have an enormous application state object but each client only needs to know about different pieces of this data.

When a specific client's state changes, we generate a small set of string-ified patches to send to them so that they can update (or catch-up) their local state.

This helps avoid sending a massive amount of data over whenever we have new state information to each connected client.

The intended use case was for running multiplayer game servers, but an example potential different case might be a websocket powered real-time database.

To Install

$ npm install --save client-ketchup

Usage

/*
 * On our server
 */

// Use this to generate new client state trackers
var CreateClientStateTracker = require('client-ketchup')
// Create a new client state tracker. You'll typically use one of these and add/remove different clients to it
var CST = CreateClientStateTracker({
  differ: require('minimal-object-diff').diff
})

// Add a new client
CST.add('some-client-id-1')

// Update our clients view of the world and then receive a set of JSON stringified patches that we can send over
var minimalPatches = CST.update({foo: 'bar', bazz: 'buzz'})

// Use whatever network protocol you please in order to send updates
myClients['some-client-id-1'].websocket.send(JSON.stringify(minimalPatches))

/*
 * Later on our client
 */
var patchObject = require('minimal-object-diff').patch
var minimalPatches = GetPatchesFromServerSomehow()
var myLocalState = GetLocalState()
myLocalState = patchObject(myLocalState, JSON.parse(minimalPatches))

client-ketchup only concerns itself with helping to keep track of and generate optimized diffs for your client data. The method of transport (websocket, server-sent events, carrier pidgen, etc) is up to the consumer.

Typically you'll already have your network protocol in place and client-ketchup will be added in to reduce bandwidth.

API

CST.{add, del, update}

add

Add a client to our client pool

CST.add('cuid-1')
CST.add('cuid-2', {thisIsOur: 'inital state object'})

del

Remove a client from our client pool

CST.del('cuid-1')

update

Overwrite the clients state and receive JSON patches to send to a client

Applying these patches to the old state creates the new state

CST.add('cuid3', {hello: 'world'})
var patches = CST.update('cuid2', {hello: 'mars'})

// Client `cuid3` now has a state of {hello: 'mars'}

// ... Later ... Likely on one of your clients

var patch = require('minimal-object-diff').patch

var patchedObject = patch({hello: 'world'}, JSON.parse(patches))

console.log(patchedObject)
// => {hello: 'mars'}

TODO:

  • [ ] I suppose that there should be a way to deal with clients that somehow got out of sync. Maybe accepting a state hash and verifying that it matches our authoritative client state.. And if not.. generate the appropriate patches? Let's handle that if/when it happens

See Also

License

MIT