|
| 1 | +# @libp2p/example-browser-pubsub <!-- omit in toc --> |
| 2 | + |
| 3 | +[](http://libp2p.io/) |
| 4 | +[](https://discuss.libp2p.io) |
| 5 | +[](https://codecov.io/gh/libp2p/js-libp2p-examples) |
| 6 | +[](https://github.com/libp2p/js-libp2p-examples/actions/workflows/ci.yml?query=branch%3Amain) |
| 7 | + |
| 8 | +> User libp2p pubsub in browsers |
| 9 | +
|
| 10 | +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. |
| 11 | + |
| 12 | +## Table of contents <!-- omit in toc --> |
| 13 | + |
| 14 | +- [Setup](#setup) |
| 15 | +- [Running](#running) |
| 16 | + - [Why do I need a Relay Server?](#why-do-i-need-a-relay-server) |
| 17 | + - [Start the Relay Server](#start-the-relay-server) |
| 18 | + - [Running the Example](#running-the-example) |
| 19 | +- [License](#license) |
| 20 | +- [Contribution](#contribution) |
| 21 | + |
| 22 | +## Setup |
| 23 | + |
| 24 | +1. Install example depenedencies and build the project |
| 25 | + ```console |
| 26 | + $ npm install |
| 27 | + $ npm run build |
| 28 | + ``` |
| 29 | +2. Open 2 terminal windows in the `./src` directory. |
| 30 | + |
| 31 | +## Running |
| 32 | + |
| 33 | +This example has three components. Two browser windows which will send pubsub messages and a relay server that they will use to establish the initial connection. |
| 34 | + |
| 35 | +### Why do I need a Relay Server? |
| 36 | + |
| 37 | +The only transport available to browser nodes that lets them be dialed by remote peers is the [WebRTC](https://www.npmjs.com/package/@libp2p/webrtc) transport. |
| 38 | + |
| 39 | +This transport requires an initial [handshake](https://en.wikipedia.org/wiki/Session_Description_Protocol) to be done out-of-band, during which the two peers exchange their capabilities, addresses and open ports. |
| 40 | + |
| 41 | +We use a [Circuit Relay](https://docs.libp2p.io/concepts/nat/circuit-relay/) server to establish an initial communication channel between the two browsers for this process, after which they will have negotiated a peer-to-peer connection and the relay will no longer be used. |
| 42 | + |
| 43 | +```mermaid |
| 44 | +sequenceDiagram |
| 45 | + Browser A->>Relay: Create reservation |
| 46 | + activate Relay |
| 47 | + Browser B->>Relay: Dial Browser A |
| 48 | + Relay->>Browser A: Relayed dial from Browser B |
| 49 | + Browser B->>Relay: WebRTC handshake request |
| 50 | + Relay->>Browser A: Relayed WebRTC handshake request |
| 51 | + Browser A->>Relay: WebRTC handshake response |
| 52 | + Relay->>Browser B: Relayed WebRTC handshake response |
| 53 | + deactivate Relay |
| 54 | + Browser B->>Browser A: WebRTC connection |
| 55 | +``` |
| 56 | + |
| 57 | +### Start the Relay Server |
| 58 | + |
| 59 | +For browsers to communicate, we first need to run the libp2p relay server: |
| 60 | + |
| 61 | +```console |
| 62 | +$ npm run relay |
| 63 | +``` |
| 64 | + |
| 65 | +Copy one of the multiaddresses in the output. |
| 66 | + |
| 67 | +### Running the Example |
| 68 | + |
| 69 | +Start the Vite server: |
| 70 | + |
| 71 | +```console |
| 72 | +$ npm start |
| 73 | +``` |
| 74 | + |
| 75 | +A browser window will automatically open. Let's call this `Browser A`. |
| 76 | + |
| 77 | +1. Paste the copied multiaddress from the relay server, paste it into the `Dial MultiAddr` input and click the `Connect` button |
| 78 | +2. `Browser A` is now connected to the relay server |
| 79 | +3. Copy the multiaddress located after the `Listening on` message |
| 80 | + |
| 81 | +Now open a second browser with the url `http://localhost:5173/`. Let's call this `Browser B`. |
| 82 | + |
| 83 | +1. Using the copied multiaddress from `Listening on` section in `Browser A`, paste it into the `Remote MultiAddress` input and click the `Connect` button |
| 84 | +2. `Browser B` is now connected to `Browser A` |
| 85 | + |
| 86 | +You can now shut down the relay server if you wish. |
| 87 | + |
| 88 | +1. In both `Browser A` and `Browser B`, enter the same topic name in the "Subscribe to topic" input and click the "Subscribe" button |
| 89 | +2. In either browser, enter a message in the `Send Message to Topic` field and click "Send" |
| 90 | + |
| 91 | +You should see the message appear in the output section towards the bottom of the other browser window. |
| 92 | + |
| 93 | +## License |
| 94 | + |
| 95 | +Licensed under either of |
| 96 | + |
| 97 | +- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>) |
| 98 | +- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>) |
| 99 | + |
| 100 | +## Contribution |
| 101 | + |
| 102 | +Unless you explicitly state otherwise, any contribution intentionally submitted |
| 103 | +for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 104 | +dual licensed as above, without any additional terms or conditions. |
0 commit comments