Permalink
Browse files

updated scuttlebutt section with more complete client/server example

  • Loading branch information...
substack committed Sep 17, 2012
1 parent ff7018b commit 712d3bc5da413b89358dab490a497115e27fcf50
Showing with 108 additions and 15 deletions.
  1. +21 −0 example/scuttlebutt/client.js
  2. +17 −0 example/scuttlebutt/server.js
  3. +70 −15 readme.markdown
@@ -0,0 +1,21 @@
+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);
+});
@@ -0,0 +1,17 @@
+var Model = require('scuttlebutt/model');
+var net = require('net');
+
+var m = new Model;
+m.set('count', '0');
+m.on('update', function (key, value) {
+ console.log(key + ' = ' + m.get('count'));
+});
+
+var server = net.createServer(function (stream) {
+ stream.pipe(m.createStream()).pipe(stream);
+});
+server.listen(8888);
+
+setInterval(function () {
+ m.set('count', Number(m.get('count')) + 1);
+}, 320);
View
@@ -452,41 +452,96 @@ the value that node `a` set finds its way to node `e` by way of nodes `b` and
simple streaming interface, the nodes can be placed on any process or server and
connected with any streaming transport that can handle string data.
-To make this example work over network sockets, just do:
+Next we can make a more realistic example that connects over the network and
+increments a counter variable.
+
+Here's the server which will set the initial `count` value to 0 and `count ++`
+every 320 milliseconds, printing all updates to count:
``` js
var Model = require('scuttlebutt/model');
var net = require('net');
var m = new Model;
-var s = m.createStream();
+m.set('count', '0');
+m.on('update', function (key, value) {
+ console.log(key + ' = ' + m.get('count'));
+});
-s.pipe(net.connect(8888, 'hostname')).pipe(s);
+var server = net.createServer(function (stream) {
+ stream.pipe(m.createStream()).pipe(stream);
+});
+server.listen(8888);
+
+setInterval(function () {
+ m.set('count', Number(m.get('count')) + 1);
+}, 320);
```
-to make a client node or do:
+Now we can make a client that connects to this server, updates the count on an
+interval, and prints all the updates it receives:
``` js
var Model = require('scuttlebutt/model');
var net = require('net');
var m = new Model;
+var s = m.createStream();
-var server = net.createServer(function (stream) {
- stream.pipe(m.createStream()).pipe(stream);
+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);
});
-server.listen(8888);
+
+m.on('update', function (key, value) {
+ console.log(key + ' = ' + value);
+});
+```
+
+The client is slightly trickier since it should wait until it has an update from
+somebody else to start updating the counter itself or else its counter would be
+zeroed.
+
+Once we get the server and some clients running we should see a sequence like this:
+
+```
+count = 183
+count = 184
+count = 185
+count = 186
+count = 187
+count = 188
+count = 189
+```
+
+Occasionally one some of the nodes we might see a sequence with repeated values like:
+
+```
+count = 147
+count = 148
+count = 149
+count = 149
+count = 150
+count = 151
```
-to make a server node. Then in either example call `m.get(key)` to get values,
-`m.set(key, value)`, and `m.on('update', function (key, value, source) {})` to
-listen for updates.
+These values are due to
+[scuttlebutt's](https://github.com/dominictarr/scuttlebutt)
+history-based conflict resolution algorithm which is hard at work ensuring that the state of the system across all nodes is eventually consistent.
-The terms "client" and "server" here don't affect how the state synchronization
-proceeds, just who initiates the connection. Protocols with this property are
-often called symmetric protocols. See
-[dnode](https://github.com/substack/dnode) for another example of a symmetric
-protocol.
+Note that the server in this example is just another node with the same
+privledges as the clients connected to it. The terms "client" and "server" here
+don't affect how the state synchronization proceeds, just who initiates the
+connection. Protocols with this property are often called symmetric protocols.
+See [dnode](https://github.com/substack/dnode) for another example of a
+symmetric protocol.
***

0 comments on commit 712d3bc

Please sign in to comment.