Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7094fa

Browse files
committed
fix(conflict):
1 parent 623aca8 commit f7094fa

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package-lock.json
2+
node_modules
3+

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ $ npm install
88
```
99

1010

11-
## 1. Local Area Network p2p via Multicast DNS
11+
## 1. Local Area Network Messages Broadcasting
1212

1313
Run in one terminals with different PORT or in differente machines in the same Local netowork
1414

15-
`node websocket-lan.js PORT`
15+
`node tcp_pubsub_message.js PORT`
16+
17+
and type a message ENTER to send in brocast to all other peers
1618

17-
any new peer is connected and discover automatically the others
1819

1920

21+
## 2. Local Area Network p2p via Multicast DNS
22+
23+
Run in one terminals with different PORT or in differente machines in the same Local netowork
24+
25+
`node websocket_lan.js PORT`
26+
27+
any new peer is connected and discover automatically the others
2028

21-
Donate ❤️ sats via LN ⚡ to incentivize work of my repos
2229

2330
[![image](https://raw.githubusercontent.com/st3b1t/st3b1t/main/donate.png)](https://getalby.com/p/st3b1t)
2431

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"dev": "DEBUG='libp2p:*' node websockets.js"
7+
"dev": "DEBUG='libp2p:*' node websocket_lan.js"
88
},
99
"keywords": [],
1010
"author": "st3b1t",
@@ -14,6 +14,8 @@
1414
"@chainsafe/libp2p-noise": "^15.0.0",
1515
"@chainsafe/libp2p-yamux": "^6.0.2",
1616
"@libp2p/bootstrap": "^10.0.24",
17+
"@libp2p/floodsub": "^9.0.20",
18+
"@libp2p/identify": "^2.0.2",
1719
"@libp2p/kad-dht": "^12.0.17",
1820
"@libp2p/mdns": "^10.0.24",
1921
"@libp2p/mplex": "^10.0.24",

tcp_pubsub_message.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// https://github.com/libp2p/js-libp2p/blob/main/doc/GETTING_STARTED.md
2+
import { createLibp2p } from 'libp2p'
3+
//import { webSockets } from '@libp2p/websockets'
4+
import { noise } from '@chainsafe/libp2p-noise'
5+
import { yamux } from '@chainsafe/libp2p-yamux'
6+
import { mdns } from '@libp2p/mdns' //Multicast DNS
7+
8+
import { tcp } from '@libp2p/tcp' //Multicast DNS
9+
10+
import { identify } from '@libp2p/identify' //necessario per far funzionare pubsub
11+
import { floodsub } from '@libp2p/floodsub'
12+
13+
import { stdin } from 'node:process'
14+
15+
//env DEBUG="libp2p:tcp,libp2p:websockets,libp2p:webtransport,libp2p:kad-dht,libp2p:dialer"
16+
17+
const port = Number(process.argv[2]) || 8000;
18+
const msg = process.argv[3] || '';
19+
20+
(async() => {
21+
22+
const libp2p = await createLibp2p({
23+
start: false,
24+
addresses: {
25+
listen: [`/ip4/0.0.0.0/tcp/${port}`]
26+
},
27+
transports: [
28+
tcp()
29+
],
30+
services: {
31+
identify: identify(),
32+
pubsub: floodsub()
33+
},
34+
connectionEncryption: [noise()],
35+
streamMuxers: [yamux()],
36+
peerDiscovery: [
37+
mdns({
38+
interval: 1000
39+
})
40+
]
41+
});
42+
43+
libp2p.addEventListener('peer:discovery', e => {
44+
console.log('Discovered:', e.detail.multiaddrs.toString() )
45+
});
46+
47+
libp2p.addEventListener('peer:connect', e => {
48+
console.log('Connected:', e.detail)
49+
})
50+
51+
await libp2p.start()
52+
53+
const listenAddrs = libp2p.getMultiaddrs();
54+
console.log('libp2p listen: ', listenAddrs)
55+
56+
const topic = 'pubsub_topic';
57+
58+
console.log('Subscribe:', topic);
59+
libp2p.services.pubsub.subscribe(topic);
60+
61+
62+
libp2p.services.pubsub.addEventListener('message', msg => {
63+
console.log(`Message:`, new TextDecoder().decode(msg.detail.data))
64+
})
65+
66+
console.log('Type a message to send it to all peers:')
67+
stdin.on('data', data => {
68+
69+
const msg = data.toString().trim()
70+
, msgBin = Buffer.from(`"${msg}"`);
71+
72+
libp2p.services.pubsub.publish(topic, msgBin);
73+
});
74+
75+
process.on('SIGINT', async () => {
76+
console.log('libp2p stopping...');
77+
await libp2p.stop()
78+
process.exit(0);
79+
});
80+
81+
})();
File renamed without changes.

0 commit comments

Comments
 (0)