If you don’t have Node.js or npm installed, you may need to create the node_modules directory first with mkdir node_modules, or use sudo depending on your system permissions.
2
Run the examples
Start the GUN server with examples:
cd node_modules/gun && npm start
This will start a relay peer server on port 8765. Visit http://localhost:8765 in your browser to see the examples.
3
Explore the examples
GUN comes with various example applications including:
Here’s a complete example of a realtime todo list application:
<!DOCTYPE html><html><head> <title>GUN Todo List</title> <meta name="viewport" content="width=device-width, initial-scale=1"></head><body> <h1>Thoughts</h1> <form id="form"> <input id="input" placeholder="Add a thought..."> <button>Add</button> </form> <ul id="parentList"></ul> <script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script> <script> const gun = Gun().get('thoughts'); const parentList = document.getElementById('parentList'); const input = document.getElementById('input'); const form = document.getElementById('form'); // Handle double-click to delete const dynamicEvent = e => { const target = e.target; gun.get(target.id).put(null); if (target.innerHTML === ' null' || target.innerHTML === '') { target.style.display = 'none'; } } // Listen for all thoughts gun.map().on((thought, id) => { parentList.insertAdjacentHTML('beforeend', `<li id="${id}"> ${thought}</li>`); const links = parentList.getElementsByTagName('li'); for (const link of links) { if (link.innerHTML === ' null' || link.innerHTML === '') { link.style.display = 'none'; } else { link.style.display = 'list-item'; } link.ondblclick = dynamicEvent; } }); // Add new thought on form submit form.addEventListener('submit', e => { e.preventDefault(); gun.set(input.value); input.value = ''; }); </script></body></html>
Open this app in multiple browser windows or tabs to see realtime synchronization in action! Changes in one window will instantly appear in all others.