Skip to content

Commit 72a0d20

Browse files
committed
chore: webrtc direct
1 parent 03b70f8 commit 72a0d20

File tree

13 files changed

+358
-0
lines changed

13 files changed

+358
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- js-libp2p-example-pubsub
3535
- js-libp2p-example-transports
3636
- js-libp2p-example-webrtc-private-to-private
37+
- js-libp2p-example-webrtc-private-to-public
3738
defaults:
3839
run:
3940
working-directory: examples/${{ matrix.project }}
@@ -95,6 +96,7 @@ jobs:
9596
- js-libp2p-example-pubsub
9697
- js-libp2p-example-transports
9798
- js-libp2p-example-webrtc-private-to-private
99+
- js-libp2p-example-webrtc-private-to-public
98100
steps:
99101
- uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be
100102
with:
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: github-actions@github.com
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.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# @libp2p/example-webrtc-browser-to-server
2+
3+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
5+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-examples.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-examples)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-examples/ci.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p-examples/actions/workflows/ci.yml?query=branch%3Amain)
7+
8+
This example leverages the [vite bundler](https://vitejs.dev/) to compile and serve the libp2p code in the browser. You can use other bundlers such as Webpack, but we will not be covering them here.
9+
10+
## Running the Server
11+
12+
To run the Go LibP2P WebRTC server:
13+
14+
```console
15+
npm run server
16+
```
17+
18+
Copy the multiaddress from the output.
19+
20+
## Running the Example
21+
22+
In a separate console tab, install dependencies and start the Vite server:
23+
24+
```shell
25+
npm i && npm run start
26+
```
27+
28+
The browser window will automatically open.
29+
Using the copied multiaddress from the Go server, paste it into the `Server MultiAddress` input and click the `Connect` button.
30+
Once the peer is connected, click the message section will appear. Enter a message and click the `Send` button.
31+
32+
The output should look like:
33+
34+
```text
35+
Dialing /ip4/10.0.1.5/udp/54375/webrtc/certhash/uEiADy8JubdWrAzseyzfXFyCpdRN02eWZg86tjCrTCA5dbQ/p2p/12D3KooWEG7N4bnZfFBNZE7WG6xm2P4Sr6sonMwyD4HCAqApEthb
36+
Peer connected '/ip4/10.0.1.5/udp/54375/webrtc/certhash/uEiADy8JubdWrAzseyzfXFyCpdRN02eWZg86tjCrTCA5dbQ/p2p/12D3KooWEG7N4bnZfFBNZE7WG6xm2P4Sr6sonMwyD4HCAqApEthb'
37+
Sending message 'hello'
38+
Received message 'hello'
39+
```
40+
41+
## Need help?
42+
43+
- Read the [js-libp2p documentation](https://github.com/libp2p/js-libp2p/tree/main/doc)
44+
- Check out the [js-libp2p API docs](https://libp2p.github.io/js-libp2p/)
45+
- Check out the [general libp2p documentation](https://docs.libp2p.io) for tips, how-tos and more
46+
- Read the [libp2p specs](https://github.com/libp2p/specs)
47+
- Ask a question on the [js-libp2p discussion board](https://github.com/libp2p/js-libp2p/discussions)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>js-libp2p WebRTC</title>
7+
<style>
8+
label,
9+
button {
10+
display: block;
11+
font-weight: bold;
12+
margin: 5px 0;
13+
}
14+
div {
15+
margin-bottom: 20px;
16+
}
17+
#send-section {
18+
display: none;
19+
}
20+
input[type='text'] {
21+
width: 800px;
22+
}
23+
</style>
24+
</head>
25+
<body>
26+
<div id="app">
27+
<div>
28+
<label for="peer">Server MultiAddress:</label>
29+
<input type="text" id="peer" />
30+
<button id="connect">Connect</button>
31+
</div>
32+
<div id="send-section">
33+
<label for="message">Message:</label>
34+
<input type="text" id="message" value="hello" />
35+
<button id="send">Send</button>
36+
</div>
37+
<div id="output"></div>
38+
</div>
39+
<script type="module" src="./index.js"></script>
40+
</body>
41+
</html>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { webRTCDirect } from '@libp2p/webrtc'
2+
import { multiaddr } from '@multiformats/multiaddr'
3+
import { createLibp2p } from 'libp2p'
4+
import { fromString, toString } from 'uint8arrays'
5+
6+
let stream
7+
const output = document.getElementById('output')
8+
const sendSection = document.getElementById('send-section')
9+
const appendOutput = (line) => {
10+
const div = document.createElement('div')
11+
div.appendChild(document.createTextNode(line))
12+
output.append(div)
13+
}
14+
const clean = (line) => line.replaceAll('\n', '')
15+
16+
const node = await createLibp2p({
17+
transports: [webRTCDirect()],
18+
connectionGater: {
19+
denyDialMultiaddr: () => {
20+
// by default we refuse to dial local addresses from the browser since they
21+
// are usually sent by remote peers broadcasting undialable multiaddrs but
22+
// here we are explicitly connecting to a local node so do not deny dialing
23+
// any discovered address
24+
return false
25+
}
26+
}
27+
})
28+
29+
await node.start()
30+
31+
node.addEventListener('peer:connect', (connection) => {
32+
appendOutput(`Peer connected '${node.getConnections().map(c => c.remoteAddr.toString())}'`)
33+
sendSection.style.display = 'block'
34+
})
35+
36+
window.connect.onclick = async () => {
37+
const ma = multiaddr(window.peer.value)
38+
39+
appendOutput(`Dialing '${ma}'`)
40+
stream = await node.dialProtocol(ma, '/echo/1.0.0')
41+
42+
stream.addEventListener('message', (evt) => {
43+
const response = toString(evt.data.subarray())
44+
appendOutput(`Received message '${clean(response)}'`)
45+
})
46+
}
47+
48+
window.send.onclick = async () => {
49+
const message = `${window.message.value}\n`
50+
appendOutput(`Sending message '${clean(message)}'`)
51+
stream.send(fromString(message))
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@libp2p/example-webrtc-private-to-public",
3+
"version": "1.0.0",
4+
"description": "Connect a browser to a server over WebRTC",
5+
"type": "module",
6+
"scripts": {
7+
"start": "vite",
8+
"build": "vite build",
9+
"server": "node ./server.js",
10+
"test:chrome": "npm run build && playwright test tests",
11+
"test:firefox": "npm run build && playwright test --browser firefox tests",
12+
"test": "npm run build && test-browser-example test"
13+
},
14+
"dependencies": {
15+
"@libp2p/noise": "^1.0.1",
16+
"@libp2p/webrtc": "^6.0.2",
17+
"@multiformats/multiaddr": "^13.0.1",
18+
"it-pushable": "^3.2.0",
19+
"libp2p": "^3.0.2",
20+
"vite": "^6.0.3"
21+
},
22+
"devDependencies": {
23+
"test-ipfs-example": "^1.0.0"
24+
}
25+
}

0 commit comments

Comments
 (0)