-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer2.js
More file actions
62 lines (51 loc) · 1.17 KB
/
peer2.js
File metadata and controls
62 lines (51 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const IPFS = require('ipfs')
const Room = require('ipfs-pubsub-room')
const ipfs = new IPFS({
repo: 'ipfs/peer2',
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/ip4/127.0.0.1/tcp/3345'
]
}
}
})
ipfs.on('ready', () => {
console.log('ipfs2 is ready')
const room = Room(ipfs, 'eth_bat')
room.on('peer joined', (peer) => {
console.log('new peer', peer)
})
room.on('subscribed', () => {
console.log('connected to pubsub room')
})
room.on('message', (msg) => {
console.log('received msg', msg)
})
setTimeout(() => {
console.log('made it')
room.broadcast('hellloooo this is peer2')
}, 10000)
})
ipfs.on('start', () => {
ipfs.pubsub.subscribe('apples', receiveMsg)
ipfs.pubsub.peers('apples', (err, peerIds) => {
if (err) throw err
console.log('peerIds', peerIds)
})
setTimeout(() => {
ipfs.pubsub.publish('apples', Buffer.from('yo this peer2 '), (err) => {
if (err) throw err
})
})
})
ipfs.on('error', (err) => {
throw err
})
const receiveMsg = (msg) => {
console.log('data', msg.data.toString('utf8'))
console.log('seqno', msg.seqno.toString('utf8'))
}