0.2.3 • Published 7 months ago

@constl/electron-webrtc-relay v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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

  • debug - Enables output log and rendered Electron Window with devtools enabled
  • preload - You can link your custom preload script. Since Webpack will just remove our script we can not do that. You have to add the ipcRenderer Module to the window object in your script.
  • webrtcPolicy - Define how to handle WebRTC within your project. If default is active, local connections in your network are not possible (please verify this)
  • maxWindows - Chromium only allows for a certain amount of WebRTC connections per Window. With maxWindows you can allow the creation of more than one window to improve performance in performance heavy applications

Methods

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

0.2.3

7 months ago

0.2.2

7 months ago

0.2.1

1 year ago

0.2.0

1 year ago