-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (42 loc) · 1.25 KB
/
index.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
51
52
53
54
55
56
57
58
/* eslint-disable no-console */
'use strict'
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const Gossipsub = require('libp2p-gossipsub')
const u8 = require('uint8arrays')
const createNode = async () => {
const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [NOISE],
pubsub: Gossipsub
}
})
await node.start()
return node
}
(async () => {
const topic = 'coronachan'
const [node] = await Promise.all([
createNode()
])
node.pubsub.on(topic, (msg) => {
console.log(`node received: ${u8.toString(msg.data)}`)
})
await node.pubsub.subscribe(topic)
setInterval(() => {
node.pubsub.publish(topic, u8.fromString('babooshka1'))
}, 1000)
/* process.stdin.on('readable', () => {
let variable = process.stdin.read();
variable = variable.toString().replace(/\n/, "");
variable = variable.replace(/\r/, "");
node.pubsub.publish(topic, u8.fromString(variable))
}); */
})()