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

push-model

v1.0.0

Published

A JSON-RPC server with object synchronization based on JSON-Patch

Downloads

7

Readme

Build Status

What's This?

This Node module implements a WebSocket JSON-RPC server with object synchronization capabilities based on the JSON-Pointer and JSON-Patch standards.

It is called a Push Model because it is intended to be used as part of a server-side MVC Model layer or MVVM ViewModel layer that requires the ability to push changes to clients.

The Model/ViewModel layer can handle JSON-RPC requests and return data directly to the requester, or it may choose to place data in a model object that is published to interested clients. Harmony Proxy is used to detect subsequent changes to the data, which are published incrementally as JSON Patches.

How To Use

const pm = require("push-model");
const server = require("http").createServer();
const model = {
		//properties that client can subscribe to
		prop1: ...,
		prop2: ...,

		//RPC methods
		method1: function(...args) {
			...
			return result;	//or promise
		},
		method2: ...
	};

pm.mount(server, "/path", model, acceptOrigins);

This creates an HTTP server and mounts model on the specified route. Clients can send RPC requests to this route over either HTTP or WebSocket, which invoke the corresponding methods on the model. Return values are automatically sent back as JSON-RPC responses.

Special Methods

The PUB/SUB mechanism is only available to WebSocket clients.

SUB/UNSUB

Clients call SUB/UNSUB to start/stop observing changes to the model object. The pointer parameter, a JSON Pointer, indicates which part of the model to observe.

SUB(pointer)
UNSUB(pointer)
PUB

Server calls PUB to notify clients of changes to the model. The patches parameter holds an array of JSON Patches describing a series of changes that were made to the model.

SUB(patches)

Special Return Values

ErrorResponse

A return value of type ErrorResponse will be translated into a JSON-RPC error message.

return new pm.ErrorResponse(code, message, data);

A Example Model

An MVC chat server that uses object synchronization to push chat messages to clients.

pm.mount(server, "/chat", {
	//data
	chatLog: [],

	//actions
	sendChat: function(name, message) {
		this.chatLog.push(name + ": " + message);
	}
});

Examples

Running the Chat Example

Open a command prompt in the push-model directory and run:

npm install
node examples/chat/model.js

That will start the chat model on localhost:8080. Then open the file examples/chat/chat.html in two browser windows and start chatting!

Running the Shared TodoList Example

Open a command prompt in the push-model directory and run:

npm install
node examples/sharedtodolist/model.js

Then open the file examples/sharedtodolist/todo.html in two or more browser windows. If you use Chrome, you must run a local web server because Chrome does not allow AJAX over file:// URL.

Running the Messenger Example
npm install
node examples/messenger/model.js

Then open the file examples/messenger/messenger.html in two or more browser windows. Enter a user ID and name to login to the messenger app.

Other Features

View the wiki for other features supported by the Push Model.