@achingbrain/webrtc-direct
TypeScript icon, indicating that this package has built-in type declarations

0.7.2 • Public • Published

js-libp2p-webrtc-direct

Discourse posts Build Status Dependency Status js-standard-style

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.

Table of Contents

Install

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

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 = new 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

MIT & Apache - Protocol Labs 2019

Package Sidebar

Install

npm i @achingbrain/webrtc-direct

Weekly Downloads

2

Version

0.7.2

License

Apache-2.0 OR MIT

Unpacked Size

61.2 kB

Total Files

28

Last publish

Collaborators

  • achingbrain