amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.05k stars 1.16k forks source link

Data stored on server but not in peer browser #1184

Closed jousi592 closed 2 years ago

jousi592 commented 2 years ago

I have created the express server as explained in the docs and want to be able to only update the data from the BE, but have the ability to store it across all peers.

However right now, when I connect from FE, it shown no data (even tho the data is clearly stored in the radata folder) and runs a BE endpoint to fetch data and put it to the server node. So the server node data gets updated, however on the FE, nothing happens. There is no LS or IndexedDB record of the data.

The server looks like this:

import express from "express"
import Gun from "gun"
import body_parser from "body-parser"
import cors from "cors"
import "./init_firebase.js"
import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const jsonParser = body_parser.json()
const app = express()
const port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765

app.use(cors({ origin: "*" })); // Enable any origin
app.use(Gun.serve).use(express.static(__dirname));

const server = app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

global.gun_instance = Gun({ web: server, peers: [`http://localhost:${port}`] });

// Endpoints
import update_currency_codes from "./endpoints/update_currency_codes.js"
import proxy from "./proxy.js"

app.post("/update_currency_codes", jsonParser, (req, res) => proxy(req, res, update_currency_codes))

The FE code that initiates Gun and checks for the data:

import gun_db_instance from "../constants/gun_db_instance";
import "gun/lib/load";
import "gun/lib/then";
import "gun/lib/webrtc";

export default async (keys = []) => {
  let data

  const final_key_instance = keys.reduce((next_instance, key) => next_instance.get(key), gun_db_instance)
  const does_key_exist = await final_key_instance.then()

  if (does_key_exist) data = await new Promise(
    res => final_key_instance.load(final_value => res(final_value))
  )

  return data
}

The Gun instance on FE:

import Gun from "gun";
import { BASE_GUNJS_PEERS } from "./other";

const gun_instance = Gun({ peers: BASE_GUNJS_PEERS }) // Peers are: ["http://localhost:8765/gun", "233.255.255.255:8765/gun"]

export default gun_instance

And the server endpoint that updates the nodes:

export default (req, res) => {
  if (!req.body?.currency || !req.body?.rates_info) return  res.send("No data")

  const currency_rates_instance = gun_instance.get("currency_rates")
  currency_rates_instance.get(req.body.currency).put(req.body.rates_info)

  res.send("Data saved")
}

Again, the local node gets updated, but on the FE, nothing happens. Peer connection is never established (nothing in the LS). So it seems it doesnt seem to find the node on http://localhost:8765/gun.

UPDATE: However, if I add the data update function to FE and remove it from BE, it updates everything correctly. As if in the previous case, the FE wasnt created just because I didnt run a put/set operation there.

Has there been any experience so far with updating the data on server, rather than in the browser?

PS: Thank you for your devotion to this project. I am trying to integrate it into our SAAS E-commerce platform to create a decentralized e-commerce ecosystem. So I really appreciate the work you have put into this! If I can help out in any way, let me know 👍 @amark

jousi592 commented 2 years ago

Resolved the issue, it was due to BASE_GUNJS_PEERS having the wrong peer route. Although somehow Gun still managed to connect to WS without fail. So maybe there is a bug in that.