-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: examples-browser #508
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2160175
refactor: examples-browser
vasco-santos 201c3ef
chore: add information to use signalling server
vasco-santos 1ddb73c
chore: apply suggestions from code review
vasco-santos 0557d6a
chore: update deps
vasco-santos e202cdf
docs: refactor libp2p browser example
jacobheun 247c198
docs(examples): add back websockets and boostrap nodes
jacobheun e335aa8
fix: handle edge case of connections closing early
jacobheun 1df7472
chore: fix lint
jacobheun 4d0d770
chore: update example deps and readme
jacobheun 3c9bc88
chore: update webrtc-star
jacobheun c9c2b61
chore: apply suggestions from code review
jacobheun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ docs | |
**/*.log | ||
test/repo-tests* | ||
**/bundle.js | ||
.cache | ||
|
||
# Logs | ||
logs | ||
|
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 @@ | ||
{ | ||
"presets": ["@babel/preset-env"], | ||
"plugins": ["syntax-async-functions","transform-regenerator"] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,22 +1,55 @@ | ||
# libp2p running in the Browser | ||
# libp2p in the browser | ||
|
||
One of the primary goals with libp2p P2P was to get it fully working in the browser and interopable with the versions running in Go and in Node.js. | ||
This example leverages the [Parcel.js bundler](https://parceljs.org/) to compile and serve the libp2p code in the browser. Parcel uses [Babel](https://babeljs.io/) to handle transpilation of the code. You can use other bundlers such as Webpack or Browserify, but we will not be covering them here. | ||
|
||
# 1. Setting up a simple app that lists connections to other nodes | ||
## Setup | ||
|
||
Start by installing libp2p's dependencies. | ||
In order to run the example, first install the dependencies from same directory as this README: | ||
|
||
```bash | ||
> cd ../../ | ||
> npm install | ||
> cd examples/libp2p-in-the-browser | ||
``` | ||
cd ./examples/libp2p-in-the-browser | ||
npm install | ||
``` | ||
|
||
## Signaling Server | ||
|
||
This example uses the `libp2p-webrtc-star` module, which enables libp2p browser nodes to establish direct connections to one another via a central signaling server. For this example, we are using the signaling server that ships with `libp2p-webrtc-star`. | ||
|
||
You can start the server by running `npm run server`. This will start a signaling server locally on port `9090`. If you'd like to run a signaling server outside of this example, you can see instructions on how to do so in the [`libp2p-webrtc-star` README](https://github.com/libp2p/js-libp2p-webrtc-star). | ||
|
||
When you run the server, you should see output that looks something like this: | ||
|
||
```log | ||
$ npm run server | ||
|
||
> [email protected] server | ||
> star-signal | ||
|
||
Listening on: http://0.0.0.0:9090 | ||
``` | ||
|
||
## Running the examples | ||
|
||
Then simply go into the folder [1](./1) and execute the following | ||
Once you have started the signaling server, you can run the Parcel server. | ||
|
||
```bash | ||
> cd 1 | ||
> npm install | ||
> npm start | ||
# open your browser in port :9090 | ||
``` | ||
npm start | ||
``` | ||
|
||
The output should look something like this: | ||
|
||
```log | ||
$ npm start | ||
|
||
> [email protected] start | ||
> parcel index.html | ||
|
||
Server running at http://localhost:1234 | ||
✨ Built in 1000ms. | ||
``` | ||
|
||
This will compile the code and start a server listening on port [http://localhost:1234](http://localhost:1234). Now open your browser to `http://localhost:1234`. You should see a log of your node's Peer ID, the discovered peers from the Bootstrap module, and connections to those peers as they are created. | ||
|
||
Now, if you open a second browser tab to `http://localhost:1234`, you should discover your node from the previous tab. This is due to the fact that the `libp2p-webrtc-star` transport also acts as a Peer Discovery interface. Your node will be notified of any peer that connects to the same signaling server you are connected to. Once libp2p discovers this new peer, it will attempt to establish a direct WebRTC connection. | ||
|
||
**Note**: In the example we assign libp2p to `window.libp2p`, in case you would like to play around with the API directly in the browser. You can of course make changes to `index.js` and Parcel will automatically rebuild and reload the browser tabs. |
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,23 @@ | ||
<!DOCTYPE html> | ||
|
||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>js-libp2p parcel.js browser example</title> | ||
</head> | ||
|
||
<body> | ||
<header> | ||
<h1 id="status">Starting libp2p...</h1> | ||
</header> | ||
|
||
<main> | ||
<pre id="output"></pre> | ||
</main> | ||
|
||
<script src="./index.js"></script> | ||
|
||
</body> | ||
|
||
</html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to ensure dependencies in the root of the project are installed since it depends on
libp2p: "../../"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NPM should actually handle the install for you, I've verified this works a few times after clearing node modules from the project root.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice, I did not realise.