gun icon indicating copy to clipboard operation
gun copied to clipboard

Gun.js server-side client

Open sachinsawant71 opened this issue 9 years ago • 10 comments

Is it possible to create server-side client for gun.js? I tried following code -

`var peers = [ 'http://localhost:3030/gun', ] var gun = Gun(peers);

var clientLogger = gun.get('update'); let index = 0; setInterval(function(){ console.log("alert..."); clientLogger .put({"log":"sample" + index++}); }, 3000); `

The above code, when I run it, does not throw any error but does not fire update events in gun.js

sachinsawant71 avatar Aug 23 '17 05:08 sachinsawant71

@sachinsawant71 yes, it should work. If it doesn't, it is either a bug or a problem with the code.

Question: I don't see any .on(cb) in your example? How do you know whether the data is updating or not without that?

amark avatar Aug 23 '17 07:08 amark

Think I'm having the same issue. [email protected]. Can't seem to get an instance on a server to connect to another server. Using the Heroku reference app, so maybe it's something with Heroku? I can get two servers to sync if I have a client connected to both servers.

yieme avatar Sep 01 '17 17:09 yieme

@yieme but you can't get 2 servers directly to sync with their IPs? Does it not work when done on localhost?

For instance, start node with gun on localhost port 8081 (set file: 'alice.json' in constructor), then start node with gun on localhost port 8082 (set file: 'bob.json' in constructor) where it has a peer of localhost 8081. Then have bob save data. That data should now appear in alice.

^ That should be working, which would indicate to me something is wrong when gun gets deployed to heorku or some server, which is gonna make it harder for me to debug. But of course if it is a problem, then we need to get it fixed because that is critical!!! Thanks for helping out / reporting.

amark avatar Sep 01 '17 17:09 amark

localhost:8080 and :8082 works. Something to do with CORS with heroku? Both are running herokuapp.com subdomains.

yieme avatar Sep 01 '17 23:09 yieme

I'm appear to be having the same issue as @yieme with 0.8.6. It works with 2 locally hosted node instances. Client data on localhost:8080 will sync with client data on localhost:8082 with having gun instance on the :8082 node server connect to :8080. If I push these two servers to heroku with the gun instance on the second node server doing a peer connection to the first node server, the gun instances don't sync across the servers.

angleman avatar Sep 19 '17 21:09 angleman

@angleman thanks for also confirming @yieme , and good to know people are actually pushing to heroku! Although yes, this is clearly problematic and becomes more difficult to debug. Any chance you guys have been able to run it on 2 non-heroku and non-localhost? To determine if this is specific to heroku, or if it working correctly is accidentally specific to localhost. This will need to be fixed, but other bugs currently have higher priority :(.

amark avatar Sep 20 '17 08:09 amark

@alancnet @yieme or possibly this was related to ?? https://github.com/amark/gun/issues/422 ??

amark avatar Sep 20 '17 08:09 amark

@amark I am having similar issue reported here, i'm unable to sync with peers server-side unless I create a new http server instance of gun listening on different port. This works... but would be nice to not have to create a new server instance. Although if that's the way it works server-side then I guess there is no issue here on my end. I was expecting to sync with peers server-side in the same way I sync with peers on the browser client-side.

jonmalave avatar Sep 26 '17 16:09 jonmalave

I think these issues would be solved if you used .on() in your server-side code? For example, when I run "npm start", this executes my http.js file which creates a server and listens on port 8080. In this http.js file, after creating the server, I'd have the following code:

gun.get("update").on(function(data,key) { // some code here **** });

The "some code here" portion will be triggered any time a peer updates the "update" object of your gun database

dannydiekroeger avatar Apr 20 '18 18:04 dannydiekroeger

It worked for me. Notice I passed an configuration object, not an array of peers.

const Gun = require('gun')
const gun = Gun({ peers:  ['http://localhost:8765/gun'] })
gun.get('test').once(v => console.log(v))

And as @amark said, this code should run on another port / server

vuau avatar Sep 02 '20 07:09 vuau