Gun.js集群现有数据无法同步,新数据正常同步问题求助
Hey there! Let's break down why your pre-existing Gun.js server data isn't syncing until you add new entries, and how to fix it. This is a common gotcha with Gun's default behavior—here's what's going on and actionable solutions:
Why This Happens
Gun.js doesn't automatically sync all historical data between nodes when they first connect. By default:
- When you run
puton a server right after startup, the peer connection to the other server hasn't been established yet, so that initial data never gets broadcast. - Even after clients connect the two servers, Gun won't pull existing data from peers unless there's an explicit request (like a
get+onlistener) or a new write operation that triggers sync.
Step-by-Step Solutions
1. Delay Initial put Until Peer Connections Are Established
Modify your server code to wait until Gun confirms a connection to the other peer before writing initial data. Use the up event, which fires when a node connects to a peer:
const Gun = require('gun'); const gun = Gun({ peers: ['http://your-other-server-ip:8765/gun'] }); // Wait for peer connection before syncing initial data gun.on('up', (peer) => { console.log(`Connected to peer: ${peer.url}, syncing initial data`); // Re-write your initial data to trigger sync gun.get('server-data').put({ server1Key: 'server1InitialValue', // Add all your existing initial data here }); });
This ensures your initial put happens after the peer link is active, so the data gets sent to the other server.
2. Have Clients Actively Request Historical Data
When your browser clients connect to both servers, add an explicit get + on listener for your data paths. This tells Gun to pull all existing data from peers, not just new updates:
// Client-side code const gun = Gun({ peers: [ 'http://server1-ip:8765/gun', 'http://server2-ip:8765/gun' ] }); // Request and listen to the root data path (or specific paths) gun.get('').on((fullData) => { console.log('Synced all data from peers:', fullData); }); // Or target a specific data node if you don't need everything gun.get('server-data').on((serverData) => { console.log('Synced server-specific data:', serverData); });
The on listener will receive both the existing historical data and any future updates.
3. Ensure Persisted Data Is Loaded Before Syncing
If you're using Gun's default radisk persistence, wait for local data to load fully before connecting to peers or writing initial data. Use the load event:
const gun = Gun({ web: server }); gun.on('load', () => { console.log('Local persisted data loaded successfully'); // Now connect to peers and sync initial data gun.peers['http://your-other-server-ip:8765/gun']; gun.get('server-data').put({ yourInitialData: 'value' }); });
This prevents cases where your server tries to sync before its own local data is ready.
4. Verify Network & CORS Configuration
Don't overlook the basics:
- Make sure port 8765 (Gun's default) is open on both servers, with no firewall blocking peer-to-peer traffic.
- For browser clients, ensure your servers have CORS enabled to allow cross-origin requests. Here's a quick Express example:
const express = require('express'); const Gun = require('gun'); const app = express(); // Enable CORS for all origins (adjust for production!) app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type'); next(); }); app.use(Gun.serve); const server = app.listen(8765); Gun({ web: server });
Quick Test Workflow
After applying these fixes, test again:
- Start both servers—they should now sync initial data once peer connections are established.
- Launch all clients—they'll pull existing data from both servers immediately.
- Add new data from any client/server—it will sync as expected.
内容的提问来源于stack exchange,提问作者FirstTrial

