syncing js objects across clients and server with socket.io

Syncing POJOs between clients/browsers, and your node.js server, with socket.io

This library allows you to keep data objects up to date between multiple clients and a server.

Once you have shared an object with Client.put(object), it (and any other objects it refers to) is assigned an ID, flattened for transport, and pushed to the server. Shared objects are then broadcast to all connected clients.

Example use with express.js. (For alternate socket.io setups, see socket.io's How To . )

   var pojosync = require('pojosync')
     , app = express()
     , server = require('http').createServer(app)
     , io = require('socket.io').listen(server, {log:false})
     , Dataserver = new pojosync.Server(io);

(Use browserify to create a file for use in browser, then reference it with a script tag. You will also need to add:

<script src="/socket.io/socket.io.js"></script>
//io defined by socket.io.js
var pojo = require('pojosync');
var Client = new pojo.Client(io);
var flossy = { type: 'Sheep', name: 'Flossy', age: 3 };

Saves flossy to the server and shares the object with other clients.

var farms = Client.list({type: 'Farm'}, optional_callback); 

farms will initially be an empty array, but will fill with data once the server has returned it.

flossy.farm = {type:'Farm', name: 'Greenacres' };

farms will then include the new object, and flossy will be updated on all clients.

Takes an object and shares it with the server (which broadcasts it to other clients). The object can be new, or a locally updated version of an already-shared object.

Takes an object pattern and returns an array of any objects matching that pattern. This array will be kept up to date as objects are .put.

Currently data is simply persisted on your server as a JSON file. This is simple but has its limitations - all the data must be loaded into memory at once. It should not be difficult to add persistence to other backends, such as MongoDB.