Vasco Santos 560d7084fe refactor: examples-browser (#508)
* refactor: examples-browser

* chore: add information to use signalling server

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* chore: update deps

* docs: refactor libp2p browser example

* docs(examples): add back websockets and boostrap nodes

docs(examples): redo the browser readme

* fix: handle edge case of connections closing early

* chore: fix lint

* chore: update example deps and readme

* chore: update webrtc-star

* chore: apply suggestions from code review

Co-Authored-By: Alan Shaw <alan.shaw@protocol.ai>

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
2020-01-24 14:40:40 +01:00

2.5 KiB

libp2p in the browser

This example leverages the Parcel.js bundler to compile and serve the libp2p code in the browser. Parcel uses Babel to handle transpilation of the code. You can use other bundlers such as Webpack or Browserify, but we will not be covering them here.

Setup

In order to run the example, first install the dependencies from same directory as this README:

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.

When you run the server, you should see output that looks something like this:

$ npm run server

> libp2p-in-browser@1.0.0 server
> star-signal

Listening on: http://0.0.0.0:9090

Running the examples

Once you have started the signaling server, you can run the Parcel server.

npm start

The output should look something like this:

$ npm start

> libp2p-in-browser@1.0.0 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. 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.