-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_pubsub_message.js
81 lines (61 loc) · 1.91 KB
/
tcp_pubsub_message.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// https://github.com/libp2p/js-libp2p/blob/main/doc/GETTING_STARTED.md
import { createLibp2p } from 'libp2p'
//import { webSockets } from '@libp2p/websockets'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { mdns } from '@libp2p/mdns' //Multicast DNS
import { tcp } from '@libp2p/tcp' //Multicast DNS
import { identify } from '@libp2p/identify' //necessario per far funzionare pubsub
import { floodsub } from '@libp2p/floodsub'
import { stdin } from 'node:process'
//env DEBUG="libp2p:tcp,libp2p:websockets,libp2p:webtransport,libp2p:kad-dht,libp2p:dialer"
const port = Number(process.argv[2]) || 8000;
const msg = process.argv[3] || '';
(async() => {
const libp2p = await createLibp2p({
start: false,
addresses: {
listen: [`/ip4/0.0.0.0/tcp/${port}`]
},
transports: [
tcp()
],
services: {
identify: identify(),
pubsub: floodsub()
},
connectionEncryption: [noise()],
streamMuxers: [yamux()],
peerDiscovery: [
mdns({
interval: 1000
})
]
});
libp2p.addEventListener('peer:discovery', e => {
console.log('Discovered:', e.detail.multiaddrs.toString() )
});
libp2p.addEventListener('peer:connect', e => {
console.log('Connected:', e.detail)
})
await libp2p.start()
const listenAddrs = libp2p.getMultiaddrs();
console.log('libp2p listen: ', listenAddrs)
const topic = 'pubsub_topic';
console.log('Subscribe:', topic);
libp2p.services.pubsub.subscribe(topic);
libp2p.services.pubsub.addEventListener('message', msg => {
console.log(`Message:`, new TextDecoder().decode(msg.detail.data))
})
console.log('Type a message to send it to all peers:')
stdin.on('data', data => {
const msg = data.toString().trim()
, msgBin = Buffer.from(`"${msg}"`);
libp2p.services.pubsub.publish(topic, msgBin);
});
process.on('SIGINT', async () => {
console.log('libp2p stopping...');
await libp2p.stop()
process.exit(0);
});
})();