Skip to content

Commit 4ecc1db

Browse files
authored
feat: add protocol and stream muxing example (#176)
Restores old protocol and stream muxing example
1 parent bab8b69 commit 4ecc1db

File tree

15 files changed

+685
-0
lines changed

15 files changed

+685
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
- js-libp2p-example-discovery-mechanisms
3030
- js-libp2p-example-peer-and-content-routing
3131
- js-libp2p-example-pnet
32+
- js-libp2p-example-protocol-and-stream-muxing
3233
- js-libp2p-example-webrtc-private-to-private
3334
defaults:
3435
run:
@@ -86,6 +87,7 @@ jobs:
8687
- js-libp2p-example-discovery-mechanisms
8788
- js-libp2p-example-peer-and-content-routing
8889
- js-libp2p-example-pnet
90+
- js-libp2p-example-protocol-and-stream-muxing
8991
- js-libp2p-example-webrtc-private-to-private
9092
steps:
9193
- uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ⚠️ IMPORTANT ⚠️
2+
3+
# Please do not create a Pull Request for this repository
4+
5+
The contents of this repository are automatically synced from the parent [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) so any changes made to the standalone repository will be lost after the next sync.
6+
7+
Please open a PR against [js-libp2p Examples](https://github.com/libp2p/js-libp2p-examples) instead.
8+
9+
## Contributing
10+
11+
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**.
12+
13+
1. Fork the [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples)
14+
2. Create your Feature Branch (`git checkout -b feature/amazing-example`)
15+
3. Commit your Changes (`git commit -a -m 'feat: add some amazing example'`)
16+
4. Push to the Branch (`git push origin feature/amazing-example`)
17+
5. Open a Pull Request
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: pull
2+
3+
on:
4+
workflow_dispatch
5+
6+
jobs:
7+
sync:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Pull from another repository
12+
uses: ipfs-examples/actions-pull-directory-from-repo@main
13+
with:
14+
source-repo: libp2p/js-libp2p-examples
15+
source-folder-path: examples/${{ github.event.repository.name }}
16+
source-branch: main
17+
target-branch: main
18+
git-username: github-actions
19+
git-email: [email protected]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* eslint-disable no-console */
2+
3+
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { tcp } from '@libp2p/tcp'
6+
import { pipe } from 'it-pipe'
7+
import { createLibp2p } from 'libp2p'
8+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
10+
11+
const createNode = async () => {
12+
const node = await createLibp2p({
13+
addresses: {
14+
listen: ['/ip4/0.0.0.0/tcp/0']
15+
},
16+
transports: [tcp()],
17+
streamMuxers: [yamux()],
18+
connectionEncrypters: [noise()]
19+
})
20+
21+
return node
22+
}
23+
24+
const [node1, node2] = await Promise.all([
25+
createNode(),
26+
createNode()
27+
])
28+
29+
// exact matching
30+
node2.handle('/your-protocol', ({ stream }) => {
31+
pipe(
32+
stream,
33+
async function (source) {
34+
for await (const msg of source) {
35+
console.log(uint8ArrayToString(msg.subarray()))
36+
}
37+
}
38+
)
39+
})
40+
41+
// multiple protocols
42+
/*
43+
node2.handle(['/another-protocol/1.0.0', '/another-protocol/2.0.0'], ({ protocol, stream }) => {
44+
if (protocol === '/another-protocol/2.0.0') {
45+
// handle backwards compatibility
46+
}
47+
48+
pipe(
49+
stream,
50+
async function (source) {
51+
for await (const msg of source) {
52+
console.log(uint8ArrayToString(msg))
53+
}
54+
}
55+
)
56+
})
57+
*/
58+
59+
const stream = await node1.dialProtocol(node2.getMultiaddrs(), ['/your-protocol'])
60+
await pipe(
61+
[uint8ArrayFromString('my own protocol, wow!')],
62+
stream
63+
)
64+
65+
/*
66+
const stream = node1.dialProtocol(node2.peerId, ['/another-protocol/1.0.0'])
67+
68+
await pipe(
69+
['my own protocol, wow!'],
70+
stream
71+
)
72+
*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable no-console */
2+
3+
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { tcp } from '@libp2p/tcp'
6+
import { pipe } from 'it-pipe'
7+
import { createLibp2p } from 'libp2p'
8+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
10+
11+
const createNode = async () => {
12+
const node = await createLibp2p({
13+
addresses: {
14+
listen: ['/ip4/0.0.0.0/tcp/0']
15+
},
16+
transports: [tcp()],
17+
streamMuxers: [yamux()],
18+
connectionEncrypters: [noise()]
19+
})
20+
21+
return node
22+
}
23+
24+
const [node1, node2] = await Promise.all([
25+
createNode(),
26+
createNode()
27+
])
28+
29+
// Add node's 2 data to the PeerStore
30+
await node1.peerStore.patch(node2.peerId, {
31+
multiaddrs: node2.getMultiaddrs()
32+
})
33+
34+
node2.handle(['/a', '/b'], ({ protocol, stream }) => {
35+
pipe(
36+
stream,
37+
async function (source) {
38+
for await (const msg of source) {
39+
console.log(`from: ${protocol}, msg: ${uint8ArrayToString(msg.subarray())}`)
40+
}
41+
}
42+
).finally(() => {
43+
// clean up resources
44+
stream.close()
45+
})
46+
})
47+
48+
const stream1 = await node1.dialProtocol(node2.peerId, ['/a'])
49+
await pipe(
50+
[uint8ArrayFromString('protocol (a)')],
51+
stream1
52+
)
53+
54+
const stream2 = await node1.dialProtocol(node2.peerId, ['/b'])
55+
await pipe(
56+
[uint8ArrayFromString('protocol (b)')],
57+
stream2
58+
)
59+
60+
const stream3 = await node1.dialProtocol(node2.peerId, ['/b'])
61+
await pipe(
62+
[uint8ArrayFromString('another stream on protocol (b)')],
63+
stream3
64+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* eslint-disable no-console */
2+
3+
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { tcp } from '@libp2p/tcp'
6+
import { pipe } from 'it-pipe'
7+
import { createLibp2p } from 'libp2p'
8+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
9+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
10+
11+
const createNode = async () => {
12+
const node = await createLibp2p({
13+
addresses: {
14+
listen: ['/ip4/0.0.0.0/tcp/0']
15+
},
16+
transports: [tcp()],
17+
streamMuxers: [yamux()],
18+
connectionEncrypters: [noise()]
19+
})
20+
21+
return node
22+
}
23+
24+
const [node1, node2] = await Promise.all([
25+
createNode(),
26+
createNode()
27+
])
28+
29+
// Add node's 2 data to the PeerStore
30+
await node1.peerStore.patch(node2.peerId, {
31+
multiaddrs: node2.getMultiaddrs()
32+
})
33+
34+
node1.handle('/node-1', ({ stream }) => {
35+
pipe(
36+
stream,
37+
async function (source) {
38+
for await (const msg of source) {
39+
console.log(uint8ArrayToString(msg.subarray()))
40+
}
41+
}
42+
)
43+
})
44+
45+
node2.handle('/node-2', ({ stream }) => {
46+
pipe(
47+
stream,
48+
async function (source) {
49+
for await (const msg of source) {
50+
console.log(uint8ArrayToString(msg.subarray()))
51+
}
52+
}
53+
)
54+
})
55+
56+
const stream1 = await node1.dialProtocol(node2.peerId, ['/node-2'])
57+
await pipe(
58+
[uint8ArrayFromString('from 1 to 2')],
59+
stream1
60+
)
61+
62+
const stream2 = await node2.dialProtocol(node1.peerId, ['/node-1'])
63+
await pipe(
64+
[uint8ArrayFromString('from 2 to 1')],
65+
stream2
66+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
 (0)