Skip to content

libp2p/js-libp2p-webrtc-direct

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@libp2p/webrtc-direct

libp2p.io IRC Discuss codecov CI

Dial using WebRTC without the need to set up any Signalling Rendezvous Point!

Table of contents

Install

$ npm i @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.

Usage

import { WebRTCDirect } from '@libp2p/webrtc-direct'
import { Multiaddr } from '@multiformats/multiaddr'
import { pipe } from 'it-pipe'
import all from 'it-all'

const ECHO_PROTOCOL = '/echo/1.0.0'
const addr = multiaddr('/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct')
const webRTCDirect = new WebRTCDirect()

const listener = webRTCDirect.createListener({
  handler: (connection) => {
    console.log('new connection opened')

    connection.newStream([ECHO_PROTOCOL])
      .then(({ stream }) => {
        void pipe(stream, stream)
      })
  },
  upgrader
})

await listener.listen(addr)
console.log('listening')

const connection = await webRTCDirect.dial(addr, {
  upgrader
})
const { stream } = await connection.newStream([ECHO_PROTOCOL])
const values = await pipe(
  [uint8arrayFromString('hello')],
  stream,
  (source) => all(source)
)
console.log(`Value: ${uint8ArrayToString(values[0])}`)

// 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.

API

Transport

Connection

Contribute

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.

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.