Skip to content
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 11 commits into from
Jan 24, 2020
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ docs
**/*.log
test/repo-tests*
**/bundle.js
.cache

# Logs
logs
Expand Down
4 changes: 4 additions & 0 deletions examples/libp2p-in-the-browser/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["syntax-async-functions","transform-regenerator"]
}
1 change: 0 additions & 1 deletion examples/libp2p-in-the-browser/1/.gitignore

This file was deleted.

32 changes: 0 additions & 32 deletions examples/libp2p-in-the-browser/1/package.json

This file was deleted.

14 changes: 0 additions & 14 deletions examples/libp2p-in-the-browser/1/public/index.html

This file was deleted.

94 changes: 0 additions & 94 deletions examples/libp2p-in-the-browser/1/src/browser-bundle.js

This file was deleted.

28 changes: 0 additions & 28 deletions examples/libp2p-in-the-browser/1/src/create-node.js

This file was deleted.

57 changes: 0 additions & 57 deletions examples/libp2p-in-the-browser/1/src/index.js

This file was deleted.

61 changes: 47 additions & 14 deletions examples/libp2p-in-the-browser/README.md
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:
Copy link
Member

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: "../../"

Copy link
Contributor

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.

Copy link
Member

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.


```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.
23 changes: 23 additions & 0 deletions examples/libp2p-in-the-browser/index.html
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>
Loading