- Notifications
You must be signed in to change notification settings - Fork7
run a GUN server from your command line
License
skiqh/gun-cli
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
DISCLAIMER: WORK IN PROGRESS/NOT PRODUCTION-READY
gun
runs aGUN server from your command line
GUN is a distributed, offline-first, realtime graph database enginewith built-in encryption. It's a small, easy, and fast data sync andstorage system that runs everywhere JavaScript does.
yarn global add gun-cli OR npm install -g gun-cli
start a local gun peer
gun --host 127.0.0.1 --watch foo.bar
access it from your browser
<html><scriptsrc="https://cdn.jsdelivr.net/npm/gun/gun.js"></script><script>constgun=Gun('http://127.0.0.1:8765/gun')gun.get('foo').get('bar').put('baz '+Date.now())</script></html>
seefoo.bar
update in your command line
22:41:50 foo.bar => "baz 1570135310381"
gun print flux.bar --file false --peers 127.0.0.1 --debounce 200 --timeout 2000
This will try to loadflux.bar
fromhttp://127.0.0.1:8765/gun
(extending the providedIP to a full gun URL with the default port8765
). If the query does not resolve withintwo seconds, the attempt will timeout. However, if the peer can resolve our query, theanswers that do come streaming in are debounced with a 200 ms interval, i.e. we try towait for a full.load()
of the data we're interested in.
ls ./mydomain-certs>ca.pem cert.pem key.pemgun --port 443 --certs ./mydomain-certs
Under the hood, this just usesrequire('https').createServer()
with the respectiveparamsca
,cert
, andkey
.
start a small mesh network of gun servers, each listening on a different IP and saving data in a different folder.
gun --host 127.0.0.1 --peers 127.0.0.3,127.0.0.4 --file ./data1 # 1gun --host 127.0.0.2 --peers 127.0.0.3,127.0.0.4 --file ./data2 # 2gun --host 127.0.0.3 --peers 127.0.0.1,127.0.0.2 --file ./data3 # 3gun --host 127.0.0.4 --peers 127.0.0.1,127.0.0.2 --file ./data4 # 4
connect two browsers, A and B, over this network
Browser A
<html><scriptsrc="https://cdn.jsdelivr.net/npm/gun/gun.js"></script><script>constpeers=['http://127.0.0.1:8765/gun','http://127.0.0.2:8765/gun']constgun=Gun({peers})gun.get('foo').get('heartbeat').on(heartbeat=>{consttime=newDate(heartbeat).toLocaleTimeString()console.log(`last heartbeat was at${time}`)})</script></html>
Browser B
<html><scriptsrc="https://cdn.jsdelivr.net/npm/gun/gun.js"></script><script>constpeers=['http://127.0.0.3:8765/gun','http://127.0.0.4:8765/gun']constgun=Gun({peers})setInterval(()=>{gun.get('foo').get('heartbeat').put(Date.now())},1000)</script></html>
now play around with shutting down individual peers and bringing them back online
As long as there is a path through the mesh network, the heartbeats will propagate from B to A.
But if peers 1 and 2 (or peers 3 and 4) simultainiously go down, A and B are seperatedand updates won't go through. However, GUN peers will try to reestablish the connectionto a lost peer, so as soon as you bring one of the peers back online, they will reconnectand updates will go through again.
gun [command] [options]COMMANDSserve [default] start a gun server on httpprint NODEPATH load NODEPATH and print as JSONversion print version numbers and exitGENERAL OPTIONS--file PATH ./gundata/ set file parameter of Gun()--peers STRING comma-seperated list of URLs and IPs (IPs are expanded to http://IP:8765/gun)--no-color do not use any colors in output--debug print GUN debug info--silent reduce command line output--repl go into a repl (with gun instace)[serve] OPTIONS--host STRING 0.0.0.0 set the ip to listen on--port NUMBER 8765 set the port to listen on--watch PATH log changes with gun.path(PATH).on()--certs PATH ./certs use https with cert files from PATH (key.pem, cert.pem, ca.pem)--nocerts disable auto-discovery of ./certs--webrtc false load lib/webrtc[print] OPTIONS--out FILENAME write to FILENAME instead of stdout--indent STRING indent characters for JSON output--debounce NUMBER 50 debounce .load() to resolve nested data set to 0 to disable debouncing--timeout NUMBER 1000 wait this much for answers to your request