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

amnesia

v1.0.5

Published

Easy memory sharing between different machines and/or processes for Node.js

Downloads

29

Readme

Amnesia

Easy memory sharing between machines and/or processes for Node.js

  • A single variable (property) is shared between machines and/or processes
  • When this variable value changes, it is updated on all other machines/processes
  • Supported value types are JSON, String, Boolean and Number
  • Sharing is done using a TCP socket
  • Extremely simple and small. No other module dependency

Disclaimer:

  • No guarantees of concurrent writes, last one stands
  • No persistence. On restarts, it'll sync with other peers and get the most updated value
  • It's actually copying data, not strictly sharing the same memory allocation

Install

npm install amnesia

Usage

Use like any JavaScript object. The shared value is on the "data" property

var mem = require('amnesia');
mem.conf = [/* your configuration, see example below */];
mem.data = 1; // mem.data variable in all machines will have their value set to 1

When value changes

mem.on('change', function(oldValue, newValue, remoteUpdate) {
	console.log('Value changed from', oldValue, 'to', newValue,
		(remoteUpdate ? 'remotely' : 'locally') );
	// remoteUpdate shows if the new value came from another machine (set remotely)
})

See what is happening/debug

mem.on('log', function(msg) {
	console.log(msg);
})

When it was last updated

console.log(mem.updated);

Configuration

Copy conf.json to your application directory and edit/add your ips/ports

mem.conf = require('./conf');

OR add it directly to your code

mem.conf = [
	{
		"host" : "10.0.1.2",
		"port" : 7777
	},
	{
		"host" : "10.0.1.2",
		"port" : 8888
	},
	{
		"host" : "10.0.1.6",
		"port" : 8888
	}
]

If you'll share on the same machine with different processes, duplicate the ip with different ports

All machines should have the same configuration with the current machine's ip and its peers.

How it works

It uses the Object.defineProperty to add a custom setter and getter to the data property on the mem (amnesia) object.

When a value is set (i.e. mem.data = 1) the custom setter is called.

The custom setter sets the value to the local variable and also write its value to the TCP socket for each other peer.

Other peers receive the new value and set them locally to their mem.data. The 'change' event triggers.

When a new process/machine starts (or restarts), it gets the most updated value from its peers (sync request).

Interactive example

On machine 1:

node
> var mem = require('amnesia')
> mem.conf = require('./conf')
> mem.data = { jsontest : 123 } // <-- set to some json
{ jsontest: 123 }
>

Then, on machine 2:

node
> var mem = require('amnesia')
> mem.conf = require('./conf') // <-- after adding config, a SYNC happens
> mem.data // <-- value for mem.data is already set
{ jsontest: 123 }
> mem.data = { jsontest: 456 } // <-- set a new value
>

Back to machine 1:

> mem.data // <-- new value already on machine 1
{ jsontest: 456 }
>

License

Licensed under the MIT license.