Gun 0.8.8, Node.js-to-Node.js, Node.js-to-browser
Here is a frontend simulation in jsfiddle https://jsfiddle.net/sergibondarenko/tktodk62/20/
To do a clean start I deleted local data.json
and gun
data from Chrome Local Storage.
Create a node in Node.js
const gun = new Gun({peers:['http://localhost:8080/gun', 'http://localhost:8081/gun']});
const node = gun.get('watcher/f0de26c0-a29f-11e7-8661-154b982951a4');
node.on(function (v, k) {
console.log('v:', v);
console.log('k:', k);
});
Listen in Node.js
const node = gun.get('watcher/f0de26c0-a29f-11e7-8661-154b982951a4');
node.get('stats').on(function (v, k) {
console.log('v:', v);
console.log('k:', k);
});
Get 1 result as expected, good
v: { _: { '#': 'j948ewfltvmmHthoESzM', '>': { num: 1508766155692 } },
num: 0 }
k: stats
Also, I have a listener on the browser side
<!DOCTYPE html>
<html>
<script src="http://rawgit.com/amark/gun/master/gun.js"></script>
<body>
<script>
var gun = new Gun({peers:['http://localhost:8080/gun', 'http://localhost:8081/gun']});
var node = gun.get('watcher/f0de26c0-a29f-11e7-8661-154b982951a4');
node.get('stats').on(function (v, k) {
console.log('v:', v);
console.log('k:', k);
});
</script>
</body>
</html>
And get 1 result on the node creation as expected, good
index.html:9 v: {"_":{"#":"j949102jDUdSklGduZh8",">":{"num":1508767186838}},"num":0}
index.html:10 k: stats
Then, I update the node
const node = gun.get('watcher/f0de26c0-a29f-11e7-8661-154b982951a4');
node.put({
stats: {
num: 2
},
name: 'trex'
});
And receive duplicated result on both listeners
Node.js
v: { _: { '#': 'j949102jDUdSklGduZh8', '>': { num: 1508769723940 } },
num: 2 }
k: stats
v: { _: { '#': 'j949102jDUdSklGduZh8', '>': { num: 1508769723940 } },
num: 2 }
k: stats
Browser
index.html:9 v: {"_":{"#":"j949102jDUdSklGduZh8",">":{"num":1508769723940}},"num":2}
index.html:10 k: stats
index.html:9 v: {"_":{"#":"j949102jDUdSklGduZh8",">":{"num":1508769723940}},"num":2}
index.html:10 k: stats
Why do I receive duplicates?
UPDATE
There are duplicates even if there is only one gun peer.
If I understand correctly, (And I had this question myself not long ago) then it's because Gun assumes it is in a decentralized lattice network. Each node that gets an update, rebroadcasts that update to all the nodes it knows about. Even though Gun is currently centralized through a relay server, it is built to be decentralized. So therefore, when you create a change, you would broadcast it, but right now it just goes through the relay server which broadcasts for you to every node including your own. So if you had a network of three peers, the relay server sends the update to all three (which includes you). Your peer can deduplicate and not rebroadcast the update but the other nodes will rebroadcast it assuming decentralization. So, therefore you would get one update from the server and then another from each other node.
I'm still not quite clear why I get multiple update pings when I'm the only peer. I believe it has something to do with the server generously broadcasting the change more than once just to add some redundancy to the system.