Updates
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
const HyperP2PDistributedEventBus = require('../index.js')
|
||||
const crypto = require('bare-crypto')
|
||||
const path = require('bare-path')
|
||||
const process = require('bare-process')
|
||||
|
||||
async function main () {
|
||||
console.log('Starting hyper-p2p-distributed-event-bus example...')
|
||||
|
||||
const topic = crypto.randomBytes(32)
|
||||
|
||||
const bus1 = new HyperP2PDistributedEventBus({
|
||||
topic,
|
||||
storageDir: path.join(process.cwd(), 'example-storage-bus1'),
|
||||
metadata: { instance: 'bus1' }
|
||||
})
|
||||
|
||||
const bus2 = new HyperP2PDistributedEventBus({
|
||||
topic,
|
||||
storageDir: path.join(process.cwd(), 'example-storage-bus2'),
|
||||
metadata: { instance: 'bus2' }
|
||||
})
|
||||
|
||||
await bus1.ready()
|
||||
await bus2.ready()
|
||||
|
||||
console.log('Both buses ready. Public keys:')
|
||||
console.log('bus1:', bus1.publicKeyHex)
|
||||
console.log('bus2:', bus2.publicKeyHex)
|
||||
|
||||
// Subscribe on bus2
|
||||
const unsub = bus2.subscribe('orders', (event) => {
|
||||
console.log('[bus2] Received order event:', event.payload, 'from', event.peerId.substring(0, 8))
|
||||
}, { priority: 'high' })
|
||||
|
||||
// Publish from bus1
|
||||
console.log('\nPublishing events from bus1...')
|
||||
await bus1.publish('orders', { item: 'laptop', qty: 1, priority: 'high' })
|
||||
await bus1.publish('orders', { item: 'mouse', qty: 5, priority: 'normal' })
|
||||
|
||||
// Wait for propagation
|
||||
await new Promise(resolve => setTimeout(resolve, 800))
|
||||
|
||||
// Replay on bus2
|
||||
console.log('\nReplaying history on bus2...')
|
||||
const history = await bus2.replay('orders', { limit: 10 })
|
||||
console.log('Replayed', history.length, 'events')
|
||||
|
||||
// Vector clock
|
||||
console.log('\nCurrent vector clock on bus1:', bus1.getVectorClock())
|
||||
|
||||
unsub()
|
||||
await bus1.close()
|
||||
await bus2.close()
|
||||
|
||||
console.log('\nExample completed successfully. All Bare/Pear compatible.')
|
||||
}
|
||||
|
||||
main().catch(console.error)
|
||||
Reference in New Issue
Block a user