Skip to content

Commit a65512b

Browse files
authored
feat: helia-lan-discovery example (#136)
* tmp: helia-lan-server-discovery testing * feat: mdns discovery example works * chore: rename example * chore: conform to example standards * chore: cleanup .gitignore
1 parent 4954e28 commit a65512b

File tree

6 files changed

+355
-0
lines changed

6 files changed

+355
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- helia-vue
3737
- helia-webpack
3838
- helia-create-car
39+
- helia-lan-discovery
3940
defaults:
4041
run:
4142
working-directory: examples/${{ matrix.project }}
@@ -99,6 +100,7 @@ jobs:
99100
- helia-vue
100101
- helia-webpack
101102
- helia-create-car
103+
- helia-lan-discovery
102104
steps:
103105
- uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be
104106
with:
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<p align="center">
2+
<a href="https://github.com/ipfs/helia" title="Helia">
3+
<img src="https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png" alt="Helia logo" width="300" />
4+
</a>
5+
</p>
6+
7+
<h3 align="center"><b>Helia LAN discovery</b></h3>
8+
9+
<p align="center">
10+
<img src="https://raw.githubusercontent.com/jlord/forkngo/gh-pages/badges/cobalt.png" width="200">
11+
<br>
12+
<a href="https://ipfs.github.io/helia/modules/helia.html">Explore the docs</a>
13+
·
14+
<a href="https://codesandbox.io/p/sandbox/infallible-haibt-e3lcd4">View Demo</a>
15+
·
16+
<a href="https://github.com/ipfs-examples/helia-examples/issues">Report Bug</a>
17+
·
18+
<a href="https://github.com/ipfs-examples/helia-examples/issues">Request Feature/Example</a>
19+
</p>
20+
21+
## Table of Contents
22+
23+
- [Table of Contents](#table-of-contents)
24+
- [About The Project](#about-the-project)
25+
- [Getting Started](#getting-started)
26+
- [Prerequisites](#prerequisites)
27+
- [Installation and Running example](#installation-and-running-example)
28+
- [Usage](#usage)
29+
- [Documentation](#documentation)
30+
- [Contributing](#contributing)
31+
- [Want to hack on IPFS?](#want-to-hack-on-ipfs)
32+
33+
## About The Project
34+
35+
- Read the [docs](https://ipfs.github.io/helia/modules/helia.html)
36+
- Look into other [examples](https://github.com/ipfs-examples/helia-examples) to learn how to spawn a Helia node in Node.js and in the Browser
37+
- Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it
38+
- Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs
39+
- Check out https://docs.ipfs.io for tips, how-tos and more
40+
- See https://blog.ipfs.io for news and more
41+
- Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io
42+
43+
## Getting Started
44+
45+
### Prerequisites
46+
47+
Make sure you have installed all of the following prerequisites on your development machine:
48+
49+
- Git - [Download & Install Git](https://git-scm.com/downloads). OSX and Linux machines typically have this already installed.
50+
- Node.js - [Download & Install Node.js](https://nodejs.org/en/download/) and the npm package manager.
51+
52+
### Installation and Running example
53+
54+
```console
55+
> npm install
56+
57+
# then in one terminal
58+
> npm run server
59+
60+
# in another terminal
61+
> npm run client
62+
```
63+
64+
To run the test
65+
66+
```console
67+
npm run test
68+
```
69+
70+
You should see all of the output from the server and client nodes, and the test should pass.
71+
72+
## Usage
73+
74+
This example shows how you can use mdns to connect two nodes. Either server/client node can be run first.
75+
76+
Both scripts (src/server.js & src/client.js) will create a helia node, and subscribe to a known pubsub topic, and shut each other down (for ease of testing).
77+
78+
Note: No WAN functionality is enabled, so only nodes on your local network can help with peer-discovery, and only nodes on your local network can be discovered as the code currently stands.. If you want to enable connecting to nodes outside of your WAN, you will need to connect to a bootstrap node.
79+
80+
### General flow
81+
82+
When you run these two scripts, the general flow works like this:
83+
84+
1. Each node subscribes to the known pubsub topic.
85+
1. When the client node detects a subscription change on the pubsub topic, it will send a `wut-CID` message to the server node.
86+
1. The server node will respond to the `wut-CID` message with the string representation of a CID the server node is providing.
87+
1. The client node will request the content for that CID via `heliaDagCbor.get(CID.parse(msg))`
88+
1. Once the content is received, the client will publish a `done` message to the pubsub topic.
89+
1. The server node will detect the `done` message, and respond with a `done-ACK` message.
90+
1. The client node will detect the `done-ACK` message, respond with a `done-ACK` message of it's own, and shutdown after a timeout (to allow for the message to be sent)
91+
1. The server node will detect the `done-ACK` message, and shutdown immediately.
92+
93+
### Testing
94+
95+
Both scripts should be able to be run in any order, and the flow should work as expected. You can run `npm run test` to check this. The test will fail if the test runs for more than 10 seconds, or errors, but this failure mode is dependent upon the `timeout` command currently.
96+
97+
### Further exploration of this example
98+
99+
Some things to try:
100+
101+
1. See if you can get ping/pong messages working without the nodes shutting down
102+
1. Run the server node from one computer on your local network, and the client node from another computer on your local network
103+
1. Try removing the shutdown code from the scripts, and see if you can get multiple clients to connect
104+
1. See if you can get the server to respond with a list of CIDs in it's blockstore, and have the client choose which one to request
105+
1. See if you can connect to bootstrap nodes with one of your nodes, and use the other node as a LAN only node.
106+
107+
108+
_For more examples, please refer to the [Documentation](#documentation)_
109+
110+
## Documentation
111+
112+
- [IPFS Primer](https://dweb-primer.ipfs.io/)
113+
- [IPFS Docs](https://docs.ipfs.io/)
114+
- [Tutorials](https://proto.school)
115+
- [More examples](https://github.com/ipfs-examples/helia-examples)
116+
- [API - Helia](https://ipfs.github.io/helia/modules/helia.html)
117+
- [API - @helia/unixfs](https://ipfs.github.io/helia-unixfs/modules/helia.html)
118+
119+
## Contributing
120+
121+
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.
122+
123+
1. Fork the IPFS Project
124+
2. Create your Feature Branch (`git checkout -b feature/amazing-feature`)
125+
3. Commit your Changes (`git commit -a -m 'feat: add some amazing feature'`)
126+
4. Push to the Branch (`git push origin feature/amazing-feature`)
127+
5. Open a Pull Request
128+
129+
## Want to hack on IPFS?
130+
131+
[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)
132+
133+
The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:
134+
135+
Read the [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md) and [JavaScript Contributing Guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md).
136+
137+
- **Check out existing issues** The [issue list](https://github.com/ipfs/helia/issues) has many that are marked as ['help wanted'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22help+wanted%22) or ['difficulty:easy'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Adifficulty%3Aeasy) which make great starting points for development, many of which can be tackled with no prior IPFS knowledge
138+
- **Look at the [Helia Roadmap](https://github.com/ipfs/helia/blob/main/ROADMAP.md)** This are the high priority items being worked on right now
139+
- **Perform code reviews** More eyes will help
140+
a. speed the project along
141+
b. ensure quality, and
142+
c. reduce possible future bugs
143+
- **Add tests**. There can never be enough tests
144+
145+
[cid]: https://docs.ipfs.tech/concepts/content-addressing "Content Identifier"
146+
[Uint8Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array
147+
[libp2p]: https://libp2p.io
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "helia-lan-discovery",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"description": "Get two helia nodes to connect on LAN without supplying address",
7+
"scripts": {
8+
"server": "cross-env node src/server.js",
9+
"client": "cross-env node src/client.js",
10+
"test": "npm run test1 && npm run test2",
11+
"test1": "timeout 10 npm run server & npm run client",
12+
"test2": "timeout 10 npm run client & npm run server"
13+
},
14+
"dependencies": {
15+
"@chainsafe/libp2p-gossipsub": "^11.0.1",
16+
"@chainsafe/libp2p-noise": "^14.1.0",
17+
"@chainsafe/libp2p-yamux": "^6.0.1",
18+
"@helia/dag-cbor": "^1.0.3",
19+
"@libp2p/autonat": "^1.0.5",
20+
"@libp2p/circuit-relay-v2": "^1.0.7",
21+
"@libp2p/identify": "^1.0.6",
22+
"@libp2p/kad-dht": "^11.0.7",
23+
"@libp2p/mdns": "^10.0.7",
24+
"@libp2p/mplex": "^10.0.7",
25+
"@libp2p/ping": "^1.0.6",
26+
"@libp2p/tcp": "^9.0.7",
27+
"@libp2p/webrtc": "^4.0.10",
28+
"@libp2p/websockets": "^8.0.7",
29+
"@libp2p/webtransport": "^4.0.10",
30+
"helia": "^2.1.0",
31+
"libp2p": "^1.0.10"
32+
},
33+
"devDependencies": {
34+
"@types/jest": "^29.5.11",
35+
"cross-env": "^7.0.3",
36+
"jest": "^29.7.0",
37+
"ts-jest": "^29.1.1"
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-disable no-console */
2+
// client.js
3+
import { dagCbor } from '@helia/dag-cbor'
4+
import { CID } from 'multiformats/cid'
5+
import { comms, getHelia, pubSubTopic } from './utils.js'
6+
7+
const helia = await getHelia('client')
8+
const heliaDagCbor = dagCbor(helia)
9+
10+
helia.libp2p.services.pubsub.addEventListener('subscription-change', (evt) => {
11+
if (helia.libp2p.services.pubsub.getSubscribers(pubSubTopic).length !== 0) {
12+
// we're subscribed, and so is another node, so request the CID
13+
helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode('wut-CID'))
14+
}
15+
})
16+
17+
await comms(helia, pubSubTopic, 'client', async (msg) => {
18+
if (msg === 'pong') {
19+
// helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode('done'))
20+
} else if (msg === 'done-ACK') {
21+
helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode('done-ACK'))
22+
setTimeout(async () => {
23+
await helia.stop()
24+
process.exit(0)
25+
}, 500)
26+
} else {
27+
// msg is a CID (response to wut-CID request), so fetch it
28+
console.log('requesting CID: %s', msg)
29+
const data = await heliaDagCbor.get(CID.parse(msg))
30+
console.log('got CID data: ', data)
31+
helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode('done'))
32+
}
33+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-console */
2+
// server.js
3+
import { dagCbor } from '@helia/dag-cbor'
4+
import { comms, getHelia, pubSubTopic } from './utils.js'
5+
6+
const helia = await getHelia('server')
7+
8+
const heliaDagCbor = dagCbor(helia)
9+
const uuid = `${new Date().toLocaleString()}: My test string that you only know if you're in the same pubsub channel as me and request my CID`
10+
const cid = await heliaDagCbor.add(uuid)
11+
const cidString = cid.toString()
12+
13+
console.log('CID: %s', cidString)
14+
15+
await comms(helia, pubSubTopic, 'server', async (msg) => {
16+
if (msg === 'done') {
17+
helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode('done-ACK'))
18+
} else if (msg === 'ping') {
19+
helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode('pong'))
20+
} else if (msg === 'done-ACK') {
21+
// other node sent done-ack and should have shut down, now it's our turn to shut down.
22+
await helia.stop()
23+
process.exit(0)
24+
} else if (msg === 'wut-CID') {
25+
helia.libp2p.services.pubsub?.publish(pubSubTopic, new TextEncoder().encode(cidString))
26+
}
27+
})
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* eslint-disable no-console */
2+
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
3+
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { autoNAT as autoNATService } from '@libp2p/autonat'
6+
import { identify as identifyService } from '@libp2p/identify'
7+
import { kadDHT } from '@libp2p/kad-dht'
8+
import { mdns } from '@libp2p/mdns'
9+
import { ping as pingService } from '@libp2p/ping'
10+
import { tcp } from '@libp2p/tcp'
11+
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
12+
import { webSockets } from '@libp2p/websockets'
13+
import { MemoryDatastore } from 'datastore-core'
14+
import { createHelia } from 'helia'
15+
import { bitswap } from 'helia/block-brokers'
16+
import { createLibp2p } from 'libp2p'
17+
18+
// @ts-check
19+
20+
export async function getHelia (clientName) {
21+
const datastore = new MemoryDatastore()
22+
23+
const libp2p = await createLibp2p({
24+
datastore,
25+
connectionManager: {
26+
minConnections: 1
27+
},
28+
addresses: {
29+
/**
30+
* you have to make sure that listening multiaddrs are announced for mdns to work
31+
*
32+
* @see https://github.com/libp2p/js-libp2p/blob/742915567749072aa784cf179ce9810f66ac6c6e/packages/peer-discovery-mdns/src/query.ts#L87-L89
33+
* @see https://github.com/libp2p/js-libp2p/blob/742915567749072aa784cf179ce9810f66ac6c6e/packages/peer-discovery-mdns/src/mdns.ts#L92-L101
34+
*/
35+
listen: [
36+
'/ip4/0.0.0.0/webrtc',
37+
'/ip4/0.0.0.0/ws',
38+
'/ip4/0.0.0.0/tcp/0'
39+
]
40+
},
41+
transports: [
42+
webRTC(),
43+
webRTCDirect(),
44+
webSockets(),
45+
tcp()
46+
],
47+
connectionEncryption: [
48+
noise()
49+
],
50+
streamMuxers: [
51+
// mplex(),
52+
yamux()
53+
],
54+
peerDiscovery: [
55+
mdns({
56+
// broadcast: mdnsBroadcast
57+
})
58+
],
59+
services: {
60+
identify: identifyService(),
61+
ping: pingService({
62+
protocolPrefix: 'ipfs'
63+
}),
64+
dht: kadDHT(),
65+
pubsub: gossipsub(),
66+
nat: autoNATService({
67+
enabled: true
68+
})
69+
}
70+
})
71+
72+
const helia = await createHelia({
73+
blockBrokers: [
74+
bitswap()
75+
],
76+
datastore,
77+
libp2p
78+
})
79+
80+
helia.libp2p.addEventListener('peer:discovery', async (connection) => {
81+
const peer = connection.detail.id
82+
console.log('%s discovered peer: ', clientName, peer.toString())
83+
})
84+
helia.libp2p.addEventListener('peer:connect', async (connection) => {
85+
console.log('%s connected to peer: ', clientName, connection.detail.toString())
86+
})
87+
return helia
88+
}
89+
90+
export async function comms (helia, topic, prefix, onMessage) {
91+
if (helia.libp2p.services.pubsub == null) {
92+
return
93+
}
94+
if (onMessage == null) {
95+
throw new Error('onMessage is required')
96+
}
97+
console.log('%s helia.libp2p.peerId %s subscribing to topic %s', prefix, helia.libp2p.peerId.toString(), topic)
98+
99+
helia.libp2p.services.pubsub?.subscribe(topic)
100+
await helia.libp2p.services.pubsub?.addEventListener('message', async (evt) => {
101+
const messageString = new TextDecoder().decode(evt.detail.data)
102+
console.log('%s gossipsub:message received: %s', prefix, messageString)
103+
await onMessage(messageString)
104+
})
105+
}
106+
107+
export const pubSubTopic = 'helia-lan-discovery'

0 commit comments

Comments
 (0)