5.0.16 • Published 7 months ago

@libp2p/webrtc v5.0.16

Weekly downloads
-
License
Apache-2.0 OR MIT
Repository
github
Last release
7 months ago

@libp2p/webrtc

libp2p.io Discuss codecov CI

A libp2p transport using WebRTC connections

About

A libp2p transport based on WebRTC datachannels.

WebRTC is a specification that allows real-time communication between nodes - it's commonly used in browser video conferencing applications but it also provides a reliable data transport mechanism called data channels which libp2p uses to facilitate protocol streams between peers.

There are two transports exposed by this module, webRTC and webRTCDirect.

WebRTC vs WebRTC Direct

The connection establishment phase of WebRTC involves a handshake using SDP during which two peers will exchange information such as open ports, network addresses and required capabilities.

A third party is usually necessary to carry out this handshake, forwarding messages between the two peers until they can make a direct connection between themselves.

The WebRTC transport uses libp2p Circuit Relays to forward SDP messages. Once a direct connection is formed the relay plays no further part in the exchange.

WebRTC Direct uses a technique known as SDP munging to skip the handshake step, instead encoding enough information in the connection request that the responder can derive what would have been in the handshake messages and so requires no third parties to establish a connection.

A WebRTC Direct multiaddr also includes a certhash of the target peer - this is used to allow opening a connection to the remote, which would otherwise be denied due to use of a self-signed certificate.

In both cases, once the connection is established a Noise handshake is carried out to ensure that the remote peer has the private key that corresponds to the public key that makes up their PeerId, giving you both encryption and authentication.

Support

WebRTC is supported in both Node.js and browsers.

At the time of writing, WebRTC Direct is dial-only in browsers and not supported in Node.js at all.

Support in Node.js is possible but PRs will need to be opened to libdatachannel and the appropriate APIs exposed in node-datachannel.

WebRTC Direct support is available in rust-libp2p and arriving soon in go-libp2p.

See the WebRTC section of https://connectivity.libp2p.io for more information.

Example - WebRTC

WebRTC requires use of a relay to connect two nodes. The listener first discovers a relay server and makes a reservation, then the dialer can connect via the relayed address.

import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { echo } from '@libp2p/echo'
import { circuitRelayTransport, circuitRelayServer } from '@libp2p/circuit-relay-v2'
import { identify } from '@libp2p/identify'
import { webRTC } from '@libp2p/webrtc'
import { webSockets } from '@libp2p/websockets'
import * as filters from '@libp2p/websockets/filters'
import { WebRTC } from '@multiformats/multiaddr-matcher'
import delay from 'delay'
import { pipe } from 'it-pipe'
import { createLibp2p } from 'libp2p'
import type { Multiaddr } from '@multiformats/multiaddr'

// the relay server listens on a transport dialable by the listener and the
// dialer, and has a relay service configured
const relay = await createLibp2p({
  addresses: {
  listen: ['/ip4/127.0.0.1/tcp/0/ws']
  },
  transports: [
    webSockets({filter: filters.all})
  ],
  connectionEncrypters: [noise()],
  streamMuxers: [yamux()],
  services: {
    identify: identify(),
    relay: circuitRelayServer()
  }
})

// the listener has a WebSocket transport to dial the relay, a Circuit Relay
// transport to make a reservation, and a WebRTC transport to accept incoming
// WebRTC connections
const listener = await createLibp2p({
  addresses: {
    listen: [
      '/p2p-circuit',
      '/webrtc'
    ]
  },
  transports: [
    webSockets({filter: filters.all}),
    webRTC(),
    circuitRelayTransport()
  ],
  connectionEncrypters: [noise()],
  streamMuxers: [yamux()],
  services: {
    identify: identify(),
    echo: echo()
  }
})

// the listener dials the relay (or discovers a public relay via some other
// method)
await listener.dial(relay.getMultiaddrs(), {
  signal: AbortSignal.timeout(5000)
})

let webRTCMultiaddr: Multiaddr | undefined

// wait for the listener to make a reservation on the relay
while (true) {
  webRTCMultiaddr = listener.getMultiaddrs().find(ma => WebRTC.matches(ma))

  if (webRTCMultiaddr != null) {
    break
  }

  // try again later
  await delay(1000)
}

// the dialer has Circuit Relay, WebSocket and WebRTC transports to dial
// the listener via the relay, complete the SDP handshake and establish a
// direct WebRTC connection
const dialer = await createLibp2p({
  transports: [
    webSockets({filter: filters.all}),
    webRTC(),
    circuitRelayTransport()
  ],
  connectionEncrypters: [noise()],
  streamMuxers: [yamux()],
  services: {
    identify: identify(),
    echo: echo()
  }
})

// dial the listener and open an echo protocol stream
const stream = await dialer.dialProtocol(webRTCMultiaddr, dialer.services.echo.protocol, {
  signal: AbortSignal.timeout(5000)
})

// we can now stop the relay
await relay.stop()

// send/receive some data from the remote peer via a direct connection
await pipe(
  [new TextEncoder().encode('hello world')],
  stream,
  async source => {
    for await (const buf of source) {
      console.info(new TextDecoder().decode(buf.subarray()))
    }
  }
)

Example - WebRTC Direct

At the time of writing WebRTC Direct is dial-only in browsers and unsupported in Node.js.

The only implementation that supports a WebRTC Direct listener is go-libp2p and it's not yet enabled by default.

import { createLibp2p } from 'libp2p'
import { noise } from '@chainsafe/libp2p-noise'
import { multiaddr } from '@multiformats/multiaddr'
import { pipe } from 'it-pipe'
import { fromString, toString } from 'uint8arrays'
import { webRTCDirect } from '@libp2p/webrtc'

const node = await createLibp2p({
  transports: [
    webRTCDirect()
  ],
  connectionEncrypters: [
    noise()
  ]
})

await node.start()

// this multiaddr corresponds to a remote node running a WebRTC Direct listener
const ma = multiaddr('/ip4/0.0.0.0/udp/56093/webrtc-direct/certhash/uEiByaEfNSLBexWBNFZy_QB1vAKEj7JAXDizRs4_SnTflsQ')
const stream = await node.dialProtocol(ma, '/my-protocol/1.0.0', {
  signal: AbortSignal.timeout(10_000)
})

await pipe(
  [fromString(`Hello js-libp2p-webrtc\n`)],
  stream,
  async function (source) {
    for await (const buf of source) {
      console.info(toString(buf.subarray()))
    }
  }
)

Install

$ npm i @libp2p/webrtc

Browser <script> tag

Loading this module through a script tag will make it's exports available as Libp2pWebrtc in the global namespace.

<script src="https://unpkg.com/@libp2p/webrtc/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

5.0.16-c2ff2e454

7 months ago

5.0.16-0a3406a05

7 months ago

5.0.14-a657bbd2e

8 months ago

5.0.15

8 months ago

5.0.16

8 months ago

5.0.16-ad5cfd66a

8 months ago

5.0.14-d34642db1

8 months ago

5.0.14-7383821e1

8 months ago

5.0.14-717731e49

8 months ago

5.0.14

8 months ago

5.0.13-aa8de9fd3

8 months ago

5.0.13-3bc9769b8

8 months ago

5.0.13-4521cf1f7

8 months ago

5.0.13-3244ed086

8 months ago

5.0.13-75301ac7d

8 months ago

5.0.13-e6b4158c6

8 months ago

5.0.9-32c176fd5

9 months ago

5.0.8-d9c7e0f7e

9 months ago

5.0.9

9 months ago

5.0.8

9 months ago

5.0.10-934a891f9

8 months ago

5.0.10

8 months ago

5.0.11

8 months ago

5.0.12

8 months ago

5.0.13

8 months ago

5.0.7-82bd42bcf

9 months ago

5.0.10-d7a818e8e

8 months ago

5.0.8-58784abf7

9 months ago

5.0.7-a390db4a4

9 months ago

5.0.11-e99e8f448

8 months ago

5.0.7-dad979f9b

9 months ago

5.0.11-0d326d102

8 months ago

5.0.9-35b48025c

9 months ago

5.0.10-d4da56961

8 months ago

5.0.9-661d6586a

8 months ago

5.0.9-80e798cdc

8 months ago

5.0.8-24fa1d5af

9 months ago

5.0.9-b4f02a637

8 months ago

5.0.12-5d199f9b6

8 months ago

5.0.9-27b2fa6b6

8 months ago

5.0.7

9 months ago

5.0.6-c5988cce8

9 months ago

5.0.6-fa83ee1c7

9 months ago

5.0.6-0c5957836

9 months ago

5.0.6-980038477

9 months ago

5.0.6

9 months ago

5.0.5

9 months ago

5.0.4

9 months ago

5.0.5-c258b35af

9 months ago

5.0.4-7f7ec82ae

9 months ago

5.0.3-b3272cfce

9 months ago

5.0.2-21fe841f2

9 months ago

5.0.3

9 months ago

5.0.2-c628c44c5

9 months ago

5.0.2-4fd7eb2e1

9 months ago

5.0.2-6ccbb06f0

9 months ago

5.0.2-34455b5f2

9 months ago

5.0.2

9 months ago

5.0.1-18dd3cb26

9 months ago

5.0.1-1210884ed

9 months ago

5.0.1

9 months ago

5.0.0

9 months ago

4.1.10-5214dec4a

9 months ago

4.1.10-e1ca9cced

9 months ago

4.1.10-81ebe4e47

9 months ago

5.0.0-7cd984569

9 months ago

4.1.10-b6681bd25

9 months ago

4.1.10-c010d575c

9 months ago

4.1.10-71e5f7a5b

9 months ago

4.1.10-2bbaf4361

9 months ago

5.0.0-d101aac4b

9 months ago

4.1.10-a142bb642

9 months ago

4.1.9-dd7b329c4

9 months ago

4.1.10-df330695a

9 months ago

4.1.10

9 months ago

4.1.9

9 months ago

4.1.8-2265e59ba

9 months ago

4.1.8-737b3ea5b

9 months ago

4.1.8

10 months ago

4.1.8-1675adee2

10 months ago

4.1.7-e211b46cc

10 months ago

4.1.7-7655e5200

10 months ago

4.1.7-50b897139

10 months ago

4.1.7

10 months ago

4.1.5-359265a3a

10 months ago

4.1.1-ce6da9896

11 months ago

4.1.0-3b9cbf7d8

12 months ago

4.1.0-0b55625d1

12 months ago

4.1.4-7939dbd5c

10 months ago

4.1.1-d1aec4d9f

11 months ago

4.1.2-4a994c5ef

11 months ago

4.1.0-15eb66428

12 months ago

4.1.1-3319ff41e

11 months ago

4.0.34-44791342

1 year ago

4.1.0-abb9f90c7

12 months ago

4.1.6-3c8dd5bbf

10 months ago

4.1.4

10 months ago

4.1.3

11 months ago

4.1.6

10 months ago

4.1.5

10 months ago

4.1.0

12 months ago

4.1.2

11 months ago

4.1.1

12 months ago

4.0.34-9e0236627

12 months ago

4.1.0-6573cb8b0

12 months ago

4.1.2-40902d99b

11 months ago

4.1.4-1dfb74e79

10 months ago

4.1.2-a8ec2bcb7

11 months ago

4.1.1-151bc46fb

11 months ago

4.1.1-e9b6a242a

11 months ago

4.1.0-b0b6cae12

12 months ago

4.1.4-0edbfe7af

10 months ago

4.1.2-8b3114292

11 months ago

4.1.0-21cf7bc56

12 months ago

4.0.34

1 year ago

4.1.2-a82ff8221

11 months ago

4.1.2-73f2b6b6d

11 months ago

4.1.0-af85a7cad

12 months ago

4.1.2-a130993ed

11 months ago

4.1.2-34cf1f7cd

11 months ago

4.1.3-f30e2ee8d

11 months ago

4.1.2-928801a80

11 months ago

4.1.3-944935f8d

11 months ago

4.1.1-e1f0b307c

11 months ago

4.1.4-c5dba70a9

10 months ago

4.0.34-8e4fdcde9

12 months ago

4.0.33

1 year ago

4.0.32

1 year ago

4.0.31

1 year ago

4.0.30

1 year ago

4.0.29

1 year ago

4.0.28

1 year ago

4.0.26

1 year ago

4.0.25

1 year ago

4.0.24

1 year ago

4.0.23

1 year ago

4.0.22

1 year ago

4.0.21

1 year ago

4.0.20

1 year ago

4.0.19

1 year ago

4.0.18

1 year ago

4.0.17

1 year ago

4.0.16

1 year ago

4.0.15

1 year ago

4.0.14

1 year ago

4.0.13

1 year ago

4.0.12

1 year ago

4.0.11

1 year ago

4.0.10

2 years ago

4.0.10-742915567

2 years ago

4.0.9-341581166

2 years ago

4.0.9-6d11e8268

2 years ago

4.0.9

2 years ago

4.0.8-d10506189

2 years ago

4.0.8-64a915ae9

2 years ago

4.0.8-3bf6387ff

2 years ago

4.0.8-93890c8f9

2 years ago

4.0.7

2 years ago

4.0.8

2 years ago

4.0.6-16588d27c

2 years ago

4.0.7-887c6ffe1

2 years ago

3.2.8-78db573f9

2 years ago

3.2.11-adea7bbbf

2 years ago

3.2.1-e9099d40

2 years ago

2.0.10-b1024c6c

2 years ago

3.2.11-3dee5df4d

2 years ago

2.0.10-7debe031

2 years ago

4.0.1-561797a89

2 years ago

3.1.7-01acccef

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

4.0.5-bcfa15993

2 years ago

3.2.0

2 years ago

2.0.10-a41d25d4

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

3.2.3

2 years ago

3.2.11-0b4a2ee79

2 years ago

4.0.0-06e6d235f

2 years ago

3.2.11-e2267d437

2 years ago

3.1.8-0634e3b7

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.6

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

3.2.1-f1053159

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

3.2.11-74e84bc29

2 years ago

3.2.5-50442d7a

2 years ago

2.0.10-06f4901a

2 years ago

3.1.7-32825633

2 years ago

3.2.7-dfbe0cc0

2 years ago

3.1.3-eaac8943

2 years ago

3.2.0-f09ac4a7

2 years ago

3.1.3-5e85154b

2 years ago

2.0.10-5315f7bc

2 years ago

3.1.10-4db2f5f5

2 years ago

3.1.9-c4eff4c5

2 years ago

2.0.10-daeb43d8

2 years ago

3.2.9

2 years ago

4.0.3-e7167fe52

2 years ago

3.2.10-c960eb659

2 years ago

3.2.8

2 years ago

3.2.7

2 years ago

3.1.9-5294f14c

2 years ago

3.0.0-e66f4891

2 years ago

3.1.7-9a69e6f7

2 years ago

2.0.10-c999d6a7

2 years ago

3.1.3

2 years ago

3.2.11-05b52d69c

2 years ago

3.1.2

2 years ago

4.0.0-a32e70bac

2 years ago

3.1.1

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.5

2 years ago

3.2.5-69581367

2 years ago

3.1.4

2 years ago

3.1.3-9c0353cf

2 years ago

2.0.10-1f7e18b0

2 years ago

3.2.3-7534ae7b

2 years ago

4.0.0-bcf18265e

2 years ago

3.1.3-02b89323

2 years ago

3.2.11

2 years ago

3.2.6-f4fac961

2 years ago

3.2.2-cf3ae893

2 years ago

3.2.10

2 years ago

3.2.11-7877a50e0

2 years ago

3.1.3-e26848b0

2 years ago

4.0.1-bca8d6e68

2 years ago

3.1.6-89778624

2 years ago

3.2.4-50f912c2

2 years ago

3.2.0-6640116d

2 years ago

3.1.2-446fff87

2 years ago

3.1.4-6abcd22f

2 years ago

3.2.11-bb6ceb192

2 years ago

3.1.3-a1fbb7e2

2 years ago

2.0.10-6eab9c5e

2 years ago

3.1.10-88c47f51

2 years ago

3.2.0-91842c93

2 years ago

3.1.3-32212959

2 years ago

3.2.8-1d141331a

2 years ago

3.2.1-28794fe4

2 years ago

3.2.2-62a56b54

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.0-a4a10fd4

2 years ago

3.0.0

2 years ago

3.2.5-346ff5a2

2 years ago

3.1.1-58421e11

2 years ago

3.1.3-a31b420f

2 years ago

3.2.3-68504939

2 years ago

3.2.0-b57bca44

2 years ago

2.0.10-5eee70a4

2 years ago

3.1.3-6b839807

2 years ago

3.1.1-5ffa7a74

2 years ago

3.1.7-13f5b48e

2 years ago

2.0.10-e9cafd3d

2 years ago

3.2.1-f670307a

2 years ago

3.1.9-28d6722f

2 years ago

3.2.10-effcfaa8e

2 years ago

3.1.8-2b755a82

2 years ago

3.2.5-d5ef1c91

2 years ago

3.2.0-ae36e86b

2 years ago

3.2.5-16a87076

2 years ago

3.2.9-fb8a6f188

2 years ago

3.2.0-e3ab1929

2 years ago

3.1.8-7517082d

2 years ago

2.0.10-a1ec46b5

2 years ago

3.2.6-fdcb801e

2 years ago

3.2.11-9c67c5b3d

2 years ago

3.1.3-4c1a33b3

2 years ago

4.0.3-7861ed882

2 years ago

3.1.3-18567b7c

2 years ago

3.2.11-4a474d54d

2 years ago

3.1.4-4ef9c79c

2 years ago

4.0.1-cf963694f

2 years ago

3.1.4-d9948596

2 years ago

3.1.4-e664d14f

2 years ago

3.2.1-5a6a4379

2 years ago

4.0.5-10ea19700

2 years ago

3.1.0-c858ca7f

2 years ago

3.1.6-098ba082

2 years ago

3.1.4-d30f09f2

2 years ago

3.1.4-87165551

2 years ago

4.0.3-9197f10ba

2 years ago

3.2.0-72319fe6

2 years ago

3.2.3-f9d1c072

2 years ago

2.0.10-7f60b579

2 years ago

3.2.3-ab2c1f67

2 years ago

2.0.10-69c93ac5

2 years ago

3.2.11-68db79f6b

2 years ago

3.2.11-8bb6d5333

2 years ago

3.1.1-8f681db3

2 years ago

3.2.6-051154dd

2 years ago

3.1.9-10cbc8fa

2 years ago

3.2.11-8f921ee97

2 years ago

3.2.0-972b10a9

2 years ago

3.2.11-13a870cbe

2 years ago

3.2.11-6b6ba9ab7

2 years ago

3.2.1-c88de8e1

2 years ago

4.0.2-8c169db1b

2 years ago

3.1.6-24c1c248

2 years ago

3.1.9-0d228f9f

2 years ago

3.2.1-e8123d3f

2 years ago

2.0.10-57c32721

2 years ago

3.2.11-6625a27fc

2 years ago

4.0.0-273d8177c

2 years ago

3.2.5-025c082a

2 years ago

3.2.1-7d8b1551

2 years ago

3.1.6-f3fd7b62

2 years ago

3.2.11-0f5c305af

2 years ago

3.2.9-70d5efc2e

2 years ago

3.2.11-d729d66a5

2 years ago

4.0.0-551622a96

2 years ago

3.1.10-20d5f220

2 years ago

3.0.0-ef83dd1d

2 years ago

3.1.6-6a02d765

2 years ago

3.1.11

2 years ago

3.2.2-77e3cbc3

2 years ago

4.0.1-6c1f0ee81

2 years ago

3.1.4-87dc7e9f

2 years ago

3.2.5-b686fb5a

2 years ago

4.0.5-5a9362e21

2 years ago

3.1.10-0ee4f784

2 years ago

3.1.10

2 years ago

4.0.1-53224004f

2 years ago

3.2.8-8b82e68e8

2 years ago

3.2.1-c97dea04

2 years ago

4.0.2-f537b3731

2 years ago

2.0.10-c2232166

2 years ago

3.0.0-72e81dc1

2 years ago

3.1.9-46dc3ce9

2 years ago

4.0.4-738dd40f1

2 years ago

3.2.1-980857c3

2 years ago

3.1.0-8d49602f

2 years ago

3.1.5-725f5df1

2 years ago

3.2.3-b5a808af

2 years ago

3.2.5-7903d7a5

2 years ago

3.1.9-73b87c5a

2 years ago

3.0.0-fdd80820

2 years ago

3.1.7-24a5edae

2 years ago

3.1.7-63041afe

2 years ago

3.1.3-123ded59

2 years ago

3.2.11-97ab31c0c

2 years ago

3.1.10-0ce318ec

2 years ago

3.1.6-b599905c

2 years ago

4.0.0-7682861f9

2 years ago

3.1.8-a6be8f0f

2 years ago

3.1.10-d9159dd5

2 years ago

2.0.10-791f56f0

2 years ago

2.0.10-7b5c54dd

2 years ago

3.2.1-96166ada

2 years ago

3.1.3-3345f28b

2 years ago

4.0.0-8e4fbe13a

2 years ago

3.1.7-4559a624

2 years ago

3.2.11-d8f5bc211

2 years ago

3.1.10-122f1e67

2 years ago

3.1.4-7b2ddc17

2 years ago

4.0.5-09dd02987

2 years ago

3.1.3-364e0592

2 years ago

3.1.0-8f855a3c

2 years ago

3.1.7-a533cc39

2 years ago

3.2.7-d25d9510

2 years ago

3.1.11-6cb80f7d

2 years ago

2.0.10-879f4794

2 years ago

2.0.10-f427cfc9

2 years ago

2.0.10-42c1c097

2 years ago

2.0.10-85a317bb

2 years ago

2.0.10-7fb23cd3

2 years ago

2.0.10-2e561fe9

2 years ago

1.2.0

2 years ago

2.0.10-6fdaa7dc

2 years ago

2.0.10-05abd49f

2 years ago

2.0.10-ea8a0637

2 years ago

2.0.10-d853d124

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.11

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.9

2 years ago

2.0.10

2 years ago

2.0.8

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

2.0.10-ab0e3980

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

2.0.10-8b0e6bef

2 years ago

2.0.10-3dfc236e

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.0.5

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

3 years ago

0.0.0

3 years ago