dominictarr / scuttlebutt

peer-to-peer replicatable data structure
Other
1.32k stars 66 forks source link

Documentation , dosnt include client side code for connecting to a tcp server #38

Closed doronaviguy closed 9 years ago

doronaviguy commented 9 years ago

When i first i looked at readme , i couldnt find the peace of code for the clients, to connect to the the tcp server . I think it would be easier for developers , to see booth server and client example. Client side example

var Model = require('scuttlebutt/model');
var net = require('net');

var m = new Model;
var s = m.createStream();

s.pipe(net.connect(8888, 'localhost')).pipe(s);

m.on('update', function cb (key) {
    // wait until we've gotten at least one count value from the network
    if (key !== 'count') return;
    m.removeListener('update', cb);

    setInterval(function () {
        m.set('count', Number(m.get('count')) + 1);
    }, 100);
});

m.on('update', function (key, value) {
    console.log(key + ' = ' + value);
});
// source http://documentup.com/substack/stream-handbook