amark / gun

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

Gun integration with express not working #1347

Closed aaronsantiago closed 6 months ago

aaronsantiago commented 6 months ago

Running the sample (slightly modified to use imports) doesn't allow any connections to gun on either localhost:8765/gun or 233.255.255.255:8765/gun. Not sure if I'm doing something wrong or if it's straight up broken, but here's the code:

import express from "express";
import Gun from "gun";

console.log("If module not found, install express globally `npm i express -g`!");
var port    = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8765;
// var express = require('express');
// var Gun     = require('..');
// require('../axe');

var app    = express();
app.use(Gun.serve);
app.use(express.static("public"));

var server = app.listen(port);
var gun = Gun({ file: 'data', web: server });

global.Gun = Gun; /// make global to `node --inspect` - debug only
global.gun = gun; /// make global to `node --inspect` - debug only

console.log('Server started on port ' + port + ' with /gun');
bmatusiak commented 6 months ago

i just ran your script.. it does work.

i set PORT to 8282 for me

and opened up http://localhost:8282/gun.js and it served the gun.js

i went ahead and coded a index.html

<script src="./gun.js"></script>
<script>
    var gun = Gun({peers:["http://localhost:8282/gun"]})

    gun.get("hello").get("world").put("ok")
</script>

i added breakpoints into node's-GUN, lib/wire.js (line 70) , it hit the point like it should

, opened up browser and checked to see if the Websocket connects, and inspected the data

everthing works for me


are you still having issues this this?

bmatusiak commented 6 months ago

233.255.255.255:8765/gun = multicast (its for nodejs to nodejs without setup), you cant set that as a peer..

aaronsantiago commented 6 months ago

Ah, yes, being able to add a log/breakpoint to wire helped me figure out what's happening. I'm used to opening /gun on public endpoints to see if it's working but that doesn't work in the express setup.

When I was testing, I was mainly testing in a slightly different environment--and critically I was missing this line: var gun = Gun({ file: 'data', web: server }); which apparently is needed to enable websocket connections.

Thanks for your response!