-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
500 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
examples/js-libp2p-example-pubsub/.github/pull_request_template.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# ⚠️ IMPORTANT ⚠️ | ||
|
||
# Please do not create a Pull Request for this repository | ||
|
||
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. | ||
|
||
Please open a PR against [js-libp2p Examples](https://github.com/libp2p/js-libp2p-examples) instead. | ||
|
||
## Contributing | ||
|
||
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**. | ||
|
||
1. Fork the [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) | ||
2. Create your Feature Branch (`git checkout -b feature/amazing-example`) | ||
3. Commit your Changes (`git commit -a -m 'feat: add some amazing example'`) | ||
4. Push to the Branch (`git push origin feature/amazing-example`) | ||
5. Open a Pull Request |
19 changes: 19 additions & 0 deletions
19
examples/js-libp2p-example-pubsub/.github/workflows/sync.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: pull | ||
|
||
on: | ||
workflow_dispatch | ||
|
||
jobs: | ||
sync: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Pull from another repository | ||
uses: ipfs-examples/actions-pull-directory-from-repo@main | ||
with: | ||
source-repo: libp2p/js-libp2p-examples | ||
source-folder-path: examples/${{ github.event.repository.name }} | ||
source-branch: main | ||
target-branch: main | ||
git-username: github-actions | ||
git-email: github-actions@github.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { gossipsub } from '@chainsafe/libp2p-gossipsub' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { identify, identifyPush } from '@libp2p/identify' | ||
import { tcp } from '@libp2p/tcp' | ||
import { createLibp2p } from 'libp2p' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
|
||
const createNode = async () => { | ||
const node = await createLibp2p({ | ||
addresses: { | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [tcp()], | ||
streamMuxers: [yamux()], | ||
connectionEncrypters: [noise()], | ||
services: { | ||
pubsub: gossipsub(), | ||
identify: identify(), | ||
identifyPush: identifyPush() | ||
} | ||
}) | ||
|
||
return node | ||
} | ||
|
||
const topic = 'news' | ||
|
||
const [node1, node2] = await Promise.all([ | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
// Connect the two nodes | ||
await node1.dial(node2.getMultiaddrs()) | ||
|
||
node1.services.pubsub.subscribe(topic) | ||
node1.services.pubsub.addEventListener('message', (evt) => { | ||
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`) | ||
}) | ||
|
||
// Will not receive own published messages by default | ||
node2.services.pubsub.subscribe(topic) | ||
node2.services.pubsub.addEventListener('message', (evt) => { | ||
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`) | ||
}) | ||
|
||
// node2 publishes "news" every second | ||
setInterval(() => { | ||
node2.services.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!')).catch(err => { | ||
console.error(err) | ||
}) | ||
}, 1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import { gossipsub } from '@chainsafe/libp2p-gossipsub' | ||
import { noise } from '@chainsafe/libp2p-noise' | ||
import { yamux } from '@chainsafe/libp2p-yamux' | ||
import { identify, identifyPush } from '@libp2p/identify' | ||
import { tcp } from '@libp2p/tcp' | ||
import { createLibp2p } from 'libp2p' | ||
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
|
||
const createNode = async () => { | ||
const node = await createLibp2p({ | ||
addresses: { | ||
listen: ['/ip4/0.0.0.0/tcp/0'] | ||
}, | ||
transports: [tcp()], | ||
streamMuxers: [yamux()], | ||
connectionEncrypters: [noise()], | ||
services: { | ||
pubsub: gossipsub(), | ||
identify: identify(), | ||
identifyPush: identifyPush() | ||
} | ||
}) | ||
|
||
return node | ||
} | ||
|
||
const topic = 'fruit' | ||
|
||
const [node1, node2, node3] = await Promise.all([ | ||
createNode(), | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
// connect node1 to node2 and node2 to node3 | ||
await node1.dial(node2.getMultiaddrs()) | ||
await node2.dial(node3.getMultiaddrs()) | ||
|
||
// subscribe | ||
node1.services.pubsub.addEventListener('message', (evt) => { | ||
if (evt.detail.topic !== topic) { | ||
return | ||
} | ||
|
||
// Will not receive own published messages by default | ||
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)}`) | ||
}) | ||
node1.services.pubsub.subscribe(topic) | ||
|
||
node2.services.pubsub.addEventListener('message', (evt) => { | ||
if (evt.detail.topic !== topic) { | ||
return | ||
} | ||
|
||
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)}`) | ||
}) | ||
node2.services.pubsub.subscribe(topic) | ||
|
||
node3.services.pubsub.addEventListener('message', (evt) => { | ||
if (evt.detail.topic !== topic) { | ||
return | ||
} | ||
|
||
console.log(`node3 received: ${uint8ArrayToString(evt.detail.data)}`) | ||
}) | ||
node3.services.pubsub.subscribe(topic) | ||
|
||
// wait for subscriptions to propagate | ||
await hasSubscription(node1, node2, topic) | ||
await hasSubscription(node2, node3, topic) | ||
|
||
const validateFruit = (msgTopic, msg) => { | ||
const fruit = uint8ArrayToString(msg.data) | ||
const validFruit = ['banana', 'apple', 'orange'] | ||
|
||
return validFruit.includes(fruit) ? 'accept' : 'ignore' | ||
} | ||
|
||
// validate fruit | ||
node1.services.pubsub.topicValidators.set(topic, validateFruit) | ||
node2.services.pubsub.topicValidators.set(topic, validateFruit) | ||
node3.services.pubsub.topicValidators.set(topic, validateFruit) | ||
|
||
// node1 publishes "fruits" | ||
for (const fruit of ['banana', 'apple', 'car', 'orange']) { | ||
console.log('############## fruit ' + fruit + ' ##############') | ||
await node1.services.pubsub.publish(topic, uint8ArrayFromString(fruit)) | ||
} | ||
|
||
console.log('############## all messages sent ##############') | ||
|
||
async function delay (ms) { | ||
await new Promise((resolve) => { | ||
setTimeout(() => resolve(), ms) | ||
}) | ||
} | ||
|
||
// Wait for node1 to see that node2 has subscribed to the topic | ||
async function hasSubscription (node1, node2, topic) { | ||
while (true) { | ||
const subs = await node1.services.pubsub.getSubscribers(topic) | ||
|
||
if (subs.map(peer => peer.toString()).includes(node2.peerId.toString())) { | ||
return | ||
} | ||
|
||
// wait for subscriptions to propagate | ||
await delay(100) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This project is dual licensed under MIT and Apache-2.0. | ||
|
||
MIT: https://www.opensource.org/licenses/mit | ||
Apache-2.0: https://www.apache.org/licenses/license-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
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 | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Oops, something went wrong.