@libp2p/webrtc 4.0.21 → 4.0.22-1fc929c1c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,20 +26,168 @@ repo and examine the changes made.
26
26
 
27
27
  A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/) based on [WebRTC datachannels](https://webrtc.org/).
28
28
 
29
- ## Example
29
+ [WebRTC](https://www.w3.org/TR/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](https://www.w3.org/TR/webrtc/#peer-to-peer-data-api) which libp2p uses to facilitate [protocol streams](https://docs.libp2p.io/concepts/multiplex/overview/) between peers.
30
+
31
+ There are two transports exposed by this module, [webRTC](https://github.com/libp2p/specs/blob/master/webrtc/webrtc.md) and [webRTCDirect](https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md).
32
+
33
+ ## WebRTC vs WebRTC Direct
34
+
35
+ The connection establishment phase of WebRTC involves a handshake using [SDP](https://en.wikipedia.org/wiki/Session_Description_Protocol) during which two peers will exchange information such as open ports, network addresses and required capabilities.
36
+
37
+ 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.
38
+
39
+ The WebRTC transport uses libp2p [Circuit Relay](https://docs.libp2p.io/concepts/nat/circuit-relay/)s to forward SDP messages. Once a direct connection is formed the relay plays no further part in the exchange.
40
+
41
+ WebRTC Direct uses a technique known as [SDP munging](https://webrtchacks.com/not-a-guide-to-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.
42
+
43
+ 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.
44
+
45
+ In both cases, once the connection is established a [Noise handshake](https://noiseprotocol.org/noise.html) 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.
46
+
47
+ ## Support
48
+
49
+ WebRTC is supported in both Node.js and browsers.
50
+
51
+ At the time of writing, WebRTC Direct is dial-only in browsers and not supported in Node.js at all.
52
+
53
+ Support in Node.js is possible but PRs will need to be opened to [libdatachannel](https://github.com/paullouisageneau/libdatachannel) and the appropriate APIs exposed in [node-datachannel](https://github.com/murat-dogan/node-datachannel).
54
+
55
+ For both WebRTC and WebRTC Direct, support is arriving soon in go-libp2p but they are unsupported in rust-libp2p.
56
+
57
+ See the WebRTC section of <https://connectivity.libp2p.io> for more information.
58
+
59
+ ## Example - WebRTC
60
+
61
+ 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.
62
+
63
+ ```TypeScript
64
+ import { noise } from '@chainsafe/libp2p-noise'
65
+ import { yamux } from '@chainsafe/libp2p-yamux'
66
+ import { echo } from '@libp2p/echo'
67
+ import { circuitRelayTransport, circuitRelayServer } from '@libp2p/circuit-relay-v2'
68
+ import { identify } from '@libp2p/identify'
69
+ import { webRTC } from '@libp2p/webrtc'
70
+ import { webSockets } from '@libp2p/websockets'
71
+ import * as filters from '@libp2p/websockets/filters'
72
+ import { WebRTC } from '@multiformats/multiaddr-matcher'
73
+ import delay from 'delay'
74
+ import { pipe } from 'it-pipe'
75
+ import { createLibp2p } from 'libp2p'
76
+ import type { Multiaddr } from '@multiformats/multiaddr'
77
+
78
+ // the relay server listens on a transport dialable by the listener and the
79
+ // dialer, and has a relay service configured
80
+ const relay = await createLibp2p({
81
+ addresses: {
82
+ listen: ['/ip4/127.0.0.1/tcp/0/ws']
83
+ },
84
+ transports: [
85
+ webSockets({filter: filters.all})
86
+ ],
87
+ connectionEncryption: [noise()],
88
+ streamMuxers: [yamux()],
89
+ services: {
90
+ identify: identify(),
91
+ relay: circuitRelayServer()
92
+ }
93
+ })
94
+
95
+ // the listener has a WebSocket transport to dial the relay, a Circuit Relay
96
+ // transport to make a reservation, and a WebRTC transport to accept incoming
97
+ // WebRTC connections
98
+ const listener = await createLibp2p({
99
+ addresses: {
100
+ listen: ['/webrtc']
101
+ },
102
+ transports: [
103
+ webSockets({filter: filters.all}),
104
+ webRTC(),
105
+ circuitRelayTransport({
106
+ discoverRelays: 1
107
+ })
108
+ ],
109
+ connectionEncryption: [noise()],
110
+ streamMuxers: [yamux()],
111
+ services: {
112
+ identify: identify(),
113
+ echo: echo()
114
+ }
115
+ })
116
+
117
+ // the listener dials the relay (or discovers a public relay via some other
118
+ // method)
119
+ await listener.dial(relay.getMultiaddrs(), {
120
+ signal: AbortSignal.timeout(5000)
121
+ })
122
+
123
+ let webRTCMultiaddr: Multiaddr | undefined
124
+
125
+ // wait for the listener to make a reservation on the relay
126
+ while (true) {
127
+ webRTCMultiaddr = listener.getMultiaddrs().find(ma => WebRTC.matches(ma))
128
+
129
+ if (webRTCMultiaddr != null) {
130
+ break
131
+ }
132
+
133
+ // try again later
134
+ await delay(1000)
135
+ }
136
+
137
+ // the listener has Circuit Relay, WebSocket and WebRTC transports to dial
138
+ // the listener via the relay, complete the SDP handshake and establish a
139
+ // direct WebRTC connection
140
+ const dialer = await createLibp2p({
141
+ transports: [
142
+ webSockets({filter: filters.all}),
143
+ webRTC(),
144
+ circuitRelayTransport()
145
+ ],
146
+ connectionEncryption: [noise()],
147
+ streamMuxers: [yamux()],
148
+ services: {
149
+ identify: identify(),
150
+ echo: echo()
151
+ }
152
+ })
153
+
154
+ // dial the listener and open an echo protocol stream
155
+ const stream = await dialer.dialProtocol(webRTCMultiaddr, dialer.services.echo.protocol, {
156
+ signal: AbortSignal.timeout(5000)
157
+ })
158
+
159
+ // we can now stop the relay
160
+ await relay.stop()
161
+
162
+ // send/receive some data from the remote peer via a direct connection
163
+ await pipe(
164
+ [new TextEncoder().encode('hello world')],
165
+ stream,
166
+ async source => {
167
+ for await (const buf of source) {
168
+ console.info(new TextDecoder().decode(buf.subarray()))
169
+ }
170
+ }
171
+ )
172
+ ```
173
+
174
+ ## Example - WebRTC Direct
175
+
176
+ At the time of writing WebRTC Direct is dial-only in browsers and unsupported in Node.js.
177
+
178
+ The only implementation that supports a WebRTC Direct listener is go-libp2p and it's not yet enabled by default.
30
179
 
31
180
  ```TypeScript
32
181
  import { createLibp2p } from 'libp2p'
33
182
  import { noise } from '@chainsafe/libp2p-noise'
34
183
  import { multiaddr } from '@multiformats/multiaddr'
35
- import first from 'it-first'
36
184
  import { pipe } from 'it-pipe'
37
185
  import { fromString, toString } from 'uint8arrays'
38
- import { webRTC } from '@libp2p/webrtc'
186
+ import { webRTCDirect } from '@libp2p/webrtc'
39
187
 
40
188
  const node = await createLibp2p({
41
189
  transports: [
42
- webRTC()
190
+ webRTCDirect()
43
191
  ],
44
192
  connectionEncryption: [
45
193
  noise()
@@ -48,7 +196,8 @@ const node = await createLibp2p({
48
196
 
49
197
  await node.start()
50
198
 
51
- const ma = multiaddr('/ip4/0.0.0.0/udp/56093/webrtc/certhash/uEiByaEfNSLBexWBNFZy_QB1vAKEj7JAXDizRs4_SnTflsQ')
199
+ // this multiaddr corresponds to a remote node running a WebRTC Direct listener
200
+ const ma = multiaddr('/ip4/0.0.0.0/udp/56093/webrtc-direct/certhash/uEiByaEfNSLBexWBNFZy_QB1vAKEj7JAXDizRs4_SnTflsQ')
52
201
  const stream = await node.dialProtocol(ma, '/my-protocol/1.0.0', {
53
202
  signal: AbortSignal.timeout(10_000)
54
203
  })