orbit-db/examples/eventlog.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-10-04 09:27:15 +02:00
'use strict'
const IpfsDaemon = require('ipfs-daemon')
2016-10-04 09:27:15 +02:00
const OrbitDB = require('../src/OrbitDB')
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'
},
}
2016-10-04 09:27:15 +02:00
IpfsDaemon(conf)
.then((res) => {
const orbitdb = new OrbitDB(res.ipfs)
const db = orbitdb.eventlog("|orbit-db|examples|eventlog-example")
2016-10-04 09:27:15 +02:00
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)
})
}
2016-10-04 09:27:15 +02:00
setInterval(query, 1000)
})
.catch((err) => console.error(err))