diff --git a/examples/eventlog.js b/examples/eventlog.js index dd36f51..d3ee6ff 100644 --- a/examples/eventlog.js +++ b/examples/eventlog.js @@ -1,30 +1,42 @@ 'use strict' -const IpfsApi = require('ipfs-api') +const IpfsDaemon = require('ipfs-daemon') const OrbitDB = require('../src/OrbitDB') -const ipfs = IpfsApi('localhost', '5001') -const orbitdb = new OrbitDB(ipfs) -const db = orbitdb.eventlog("/orbit-db/examples/eventlog-example") - -const creatures = ['🐙', '🐷', '🐬', '🐞', '🐈', '🙉', '🐸', '🐓'] - -const query = () => { - const index = Math.floor(Math.random() * creatures.length) - db.add(creatures[index]) - .then(() => { - const latest = db.iterator({ limit: 5 }).collect() - let output = `` - output += `---------------------------------------------------\n` - output += `Latest Visitors\n` - output += `---------------------------------------------------\n` - output += latest.reverse().map((e) => e.payload.value).join('\n') + `\n` - console.log(output) - }) - .catch((e) => { - console.error(e.stack) - console.log("Make sure you have an IPFS daemon running at localhost:5001") - }) +const userId = Math.floor(Math.random() * 100) +const conf = { + IpfsDataDir: '/tmp/' + userId, + Addresses: { + API: '/ip4/127.0.0.1/tcp/0', + Swarm: ['/ip4/0.0.0.0/tcp/0'], + Gateway: '/ip4/0.0.0.0/tcp/0' + }, } -setInterval(query, 1000) +IpfsDaemon(conf) + .then((res) => { + const orbitdb = new OrbitDB(res.ipfs) + const db = orbitdb.eventlog("|orbit-db|examples|eventlog-example") + + const creatures = ['🐙', '🐷', '🐬', '🐞', '🐈', '🙉', '🐸', '🐓'] + + const query = () => { + const index = Math.floor(Math.random() * creatures.length) + db.add({ avatar: creatures[index], userId: userId }) + .then(() => { + const latest = db.iterator({ limit: 5 }).collect() + let output = `` + output += `--------------------\n` + output += `Latest Visitors\n` + output += `--------------------\n` + output += latest.reverse().map((e) => e.payload.value.avatar + " (userId: " + e.payload.value.userId + ")").join('\n') + `\n` + console.log(output) + }) + .catch((e) => { + console.error(e.stack) + }) + } + + setInterval(query, 1000) + }) + .catch((err) => console.error(err)) diff --git a/examples/start-daemon.js b/examples/start-daemon.js new file mode 100644 index 0000000..5652059 --- /dev/null +++ b/examples/start-daemon.js @@ -0,0 +1,13 @@ +const IpfsDaemon = require('ipfs-daemon') +module.exports = IpfsDaemon({ + IpfsDataDir: './tmp', + API: { + HTTPHeaders: { + "Access-Control-Allow-Origin": ['*'], + "Access-Control-Allow-Methods": ["PUT", "GET", "POST"], + "Access-Control-Allow-Credentials": ["true"] + } + } +}) +.then((res) => console.log("started")) +.catch((err) => console.error(err))