Node data exchange between server and client

Is there a way to use node and socketIo / express to somehow bind the object variable to the client side, so that it behaves like a reactive meteor and remains synchronized, like a meteor does?

In other words, for example, I have a JSON object variable in my node.js script:

var marksObject = {
    firstValue   : 'this is the first value' ,
    secondValue  : 'this is the second value'
};

I am looking for an easy way to make markObject appear on both the client and the node server and synchronized. therefore, if changes occur either on the client or on the node server, this change occurs in both places.

Ideally, this would mean only a few lines of code, and I would not have to write anything to manually synchronize the server & node server.

Redis looks very interesting, but it seems like it is redundant for what I'm trying to do.

Thank you very much.

+4
source share
2 answers

it would seem that deepstream.io does just that for us with minimal effort.

depth server:

var DeepstreamServer = require('deepstream.io');
var server = new DeepstreamServer();
server.set('host', '192.168.123.123');
server.set ('port', 6020 );
server.set ('tcpPort', 6021 );    
// this is the default, but its a nice reminder
server.start();

node "client" (runs on the server, but behaves exactly the same as the client):

var deepstream = require('deepstream.io-client-js');
ds = deepstream( '192.168.123.123:6021' ).login();   
// port has to match tcpPort or just use 6021
record = ds.record.getRecord( 'clientRecord' );
record.set('clientField', 'test from the node client running on the server! ');
record.subscribe ('clientField' , function (value)  {
            console.log('subscribed value: ' + value)
});

the client browser runs in one or more browsers

<script type='text/javascript' src='deepstream.min.js'></script>
<script type='text/javascript'>
ds = deepstream( '192.168.123.123:6020' ).login();
record = ds.record.getRecord( 'clientRecord' );
record.set ('clientField', 'test test from a browser!');
record.subscribe ('clientField' , function (value)  {
            console.log('subscribed value: ' + value)
});
//   for fun, enter     record.get('clientField');  in the console too
//   and also enter    record.set('clientField', 'this is what i entered in the console!');
</script>
+4
source

Just write a wrapper class using getters and setters. And in "set" send updates to the other side.

0
source

All Articles