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

transaction-manager

v2.1.7

Published

Simple transaction manager for json messages

Downloads

15,182

Readme

transaction-manager

Simple transaction manager for json messages

Installation

npm install --save transaction-manager

Usage example

const TransactionManager = require("../index.js");

//Use websocket as transport
var tm = new TransactionManager(websocket);

//Add listeners for commands
tm
	.on("cmd",(cmd)=> {
		console.log("got cmd", cmd.name);
		switch(cmd.name) 
		{
			case "..." :
				cmd.accept(...);
				break;
			case "..." :
				cmd.reject(...);
				break;
		}
	})
	.on("event",(event)=> {
		console.log("got event", event.name);
	});

//Send new command
tm.cmd("cmd_name", cmd_data)
	.then((data) => {
		console.log("command accepted with data", data);
	})
	.reject((data) => {
		console.log("command accepted with data", data);
	});

tm.event("event_name");

Namespacing

It is possible to provide a namespace attribute the command and events so you can route them easily between different listeners.

You can either provide a third parameter to the cmd(name,data,namespace) and event(name,data,namespace) or create a Namespace object that will handle internals for you on both sending and receiving:

var ns1 = tm1.namespace("ns");
var ns2 = tm2.namespace("ns");

ns2.on("cmd",(cmd)=> {
	console.log("ns2::got command", cmd.name);
	cmd.accept("accepted");
});


ns1.cmd("test_namespace", { dummy: 1})
	.then(() => {
		console.log("ns1::command accepted");
	})
	.catch(console.error);
	

Note that if a namespace has been created for a received message, the event will not be emited to the main TransactionManager object but only to the registered Namespace

Wire protocol

The transaction manager uses a JSON based message format to exchange data between both ends (using WebSockets for example).

The base message will be a json object with a type property:

{
	"type" : "cmd"
}

There are three different message types allowed in this specification: cmd, response and event.

Command message

The command message is a json object which type is “cmd”.It is used when the sending side expect a response from the other side as a result of this command.

Appart of the cmd attribute, it also contain the following attributes:

{
	"type"   : "cmd",
	"transId": 0,
	"name"   : "command_name",
	"data"   : ...
}
  • type (String): “cmd”
  • transId (Integer) : Unique id for this command transaction
  • name (String): Command name
  • data (String | Date | Array | Object) : custom command data

Response message

The command message is a json object which type is “response”. It is used to accept a command sent by the other peer and pass some custom data associated to the response.

Apart of the cmd attribute, it also contain the following attributes:

{
	"type"    : "response",
	"transId" : 0,
	"data"    : ...
}
  • type (String): “response”
  • transId (Integer) : value from the command this response belongs to
  • data (String | Date | Array | Object) : custom response data

Error message

The command message is a json object which type is “error”. It is used to reject a command sent by the other peer and pass some custom data associated to the error.

Apart of the cmd attribute, it also contain the following attributes:

{
	"type"    : "error",
	"transId" : 0,
	"data"    : ...
}
  • type (String): “error”
  • transId (Integer) : value from the command this error belongs to
  • data (String | Date | Array | Object) : custom error data

For each command, there must be a single response or error message matching the transaction id of the command.

Event message

Event messages are used when the sending side does not expect any kind of response back and has a type of “event”. It has the following attributes:

{
	"type": "event",
	"name": "event_name",
	"data": ... 
}
  • type (String): “event”
  • name (String): Event name
  • data (String | Date | Array | Object) : custom event data

Note that the event message does not have any transaction id as it is not meant to be acknowledged by the receiving side.

License

MIT