CA9io / electron-wrtc-relay

Drop-in replacement for wrtc in electron projects using a BrowserWindow and relaying its webrtc
https://ca9.io
MIT License
6 stars 2 forks source link
electron webrtc wrtc

Electron Webrtc Relay

Use WebRTC in the Main Process in your Electron project.

WebRTC is a powerful web API that lets browsers make peer-to-peer connections, and has already been deployed in many popular browsers. It may sometimes be useful to let Node.js programs use WebRTC, e.g. in webtorrent-hybrid. However, the modules for WebRTC in Node (node-webrtc and node-rtc-peer-connection) are either hard to install, broken, or incomplete.


This module started as a fork of electron-webrtc but removed the broken, unsafe and old electron dependencies of electron-eval and implemented some pending pull requests.


Status

This module is compatible with simple-peer and passes its tests [compatible but tests need an update ;)].

electron-webrtc-relay is intended for use with RTCDataChannels, so the MediaStream API is not supported.

Usage

npm install @ca9io/electron-webrtc-relay

// call exported function to create Electron process
var wrtc = require("@ca9io/electron-webrtc-relay")({
  debug: false, //(optional) defaults to false
  preload: string, //(optional) absolute path to your preload script. Using secure context if active (TODO: add example implementation)
  webrtcPolicy:  "default" | "default_public_interface_only" | "default_public_and_private_interfaces" | "disable_non_proxied_udp" // (optional) default: "default". Read More: https://www.electronjs.org/docs/latest/api/web-contents#contentssetwebrtciphandlingpolicypolicy
});

// IMPORTANT: WHEN YOUR APP IS LOADED CALL
wrtc.init()

// handle errors that may occur when trying to communicate with Electron
wrtc.on("error", function (err) {
  console.log(err);
});

// uses the same API as the `wrtc` package
var pc = new wrtc.RTCPeerConnection(config);

// compatible with `simple-peer`
var peer = new SimplePeer({
  initiator: true,
  wrtc: wrtc,
});

// listen for errors
wrtc.on("error", function (err, source) {
  console.error(err);
});

Configuration

var wrtc = require('@ca9io/electron-webrtc-relay')([opts])

Calling the function exported by this module will create a new hidden Electron Window.

An optional opts object may contain specific options.

The object returned by this function has the same API as the node-webrtc package.

Any errors that occur when communicating with the Electron daemon will be emitted by the wrtc object (wrtc.on('error', ...)).

wrtc.init()

Tells the relay to start a Browser window. It is important that you call this once.

wrtc.close()

Frees some resources.

Events

- error

Emitted by RTCPeerConnection or RTCDataChannel when daemon.eval() evaluates code that throws an internal error.

Related Modules