A WebRTC transport built for libp2p (not mandatory to use with libp2p) that doesn't require the set up a signalling server. Caveat, you can only establish Browser to Node.js and Node.js to Node.js connections.
> npm install libp2p-webrtc-direct
NOTE: To run build scripts node-pre-gyp
is required. You can install it by running npm install -g node-pre-gyp
.
const WebRTCDirect = require('libp2p-webrtc-direct')
const multiaddr = require('multiaddr')
const pipe = require('pull-stream')
const { collect } = require('streaming-iterables')
const addr = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
const webRTCDirect = new WebRTCDirect()
const listener = webRTCDirect.createListener((socket) => {
console.log('new connection opened')
pipe(
['hello'],
socket
)
})
await listener.listen(addr)
console.log('listening')
const conn = await webRTCDirect.dial(addr)
const values = await pipe(
conn,
collect
)
console.log(`Value: ${values.toString()}`)
// Close connection after reading
await listener.close()
Outputs:
listening
new connection opened
Value: hello
Note that it may take some time for the connection to be established.
The libp2p implementation in JavaScript is a work in progress. As such, there are a few things you can do right now to help out:
- Go through the modules and check out existing issues. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
- Perform code reviews.
- Add tests. There can never be enough tests.
MIT © Protocol Labs