Search code examples
gun

Disconnecting a gun peer


What is the correct way to disconnect gun peers? I can connect peers with:

Gun(['http://127.0.0.1:8080/gun','http://127.0.0.1:8081/gun'])

But cant disconnect again using

Gun(['http://127.0.0.1:8080/gun')

as peers will still both sync. In the last part of the video I couldn't disconnect the peers as both db's were still syncing.

On a different note the first part of the video shows problems with Chrome not updating however firefox works as it should.

Video and Source Code


Solution

  • @soth this is a good question. There is currently no standard way to disconnect from a peer. This should probably be added.

    This is terrible. But here is a hacky solution for now:

    var peers = gun.back('opt.peers');
    Object.keys(peers).forEach(function(id){
        var peer = peers[id];
        peer.retry = 0;
        gun.on('bye', peer);
    });
    

    This will disconnect from all peers. If you only want one, filter by the ID which will probably be the URL.