-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer1.js
50 lines (42 loc) · 957 Bytes
/
peer1.js
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
const IPFS = require('ipfs')
const Room = require('ipfs-pubsub-room')
const ipfs = new IPFS({
repo: 'ipfs/peer1',
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/ip4/127.0.0.1/tcp/3344'
]
}
}
})
ipfs.on('ready', () => {
console.log('ipfs1 is ready')
const room = Room(ipfs, 'eth_bat')
room.on('peer joined', (peer) => {
console.log('Peer joined room', peer)
})
room.on('subscribed', () => {
console.log('connected to pubsub room')
})
room.on('message', (msg) => {
console.log('received msg', msg)
})
})
ipfs.on('start', () => {
console.log('ipfs is ready')
ipfs.pubsub.subscribe('apples', receiveMsg)
ipfs.pubsub.peers('apples', (err, peerIds) => {
if (err) throw err
console.log('peerIds', peerIds)
})
})
ipfs.on('error', (err) => {
throw err
})
const receiveMsg = (msg) => {
console.log('peer1 received msg', msg.data.toString('utf8'))
}