1.0.6 • Published 7 years ago

electron-peer-connection v1.0.6

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

electron-peer-connection

This npm module is a wrapper for RTCPeerConnection, suited for the inter-Electron BrowserWindow communication which does not require a STUN server for signalling. Signalling is done throught Electron's built-in ipc feature instead.

Install

$ npm install --save electron-peer-connection

Example

  • main process
const p2pChannel = require('electron-peer-connection').main;

p2pChannel.initChannel();
// windowA and windowB are previously initiated BrowserWindows
p2pChannel.addClient( { window: windowA, name: 'windowA' } );
p2pChannel.addClient( { window: windowB, name: 'windowB' } );
  • window A (Sender)
const WindowPeerConnection = require("electron-peer-connection").WindowPeerConnection;

let windowA = new WindowPeerConnection('windowA');
windowA.attachStream(stream);
windowA.sendStream('windowB');
  • window B (Receiver)
const WindowPeerConnection = require("electron-peer-connection").WindowPeerConnection;

let windowB = new WindowPeerConnection('windowB');
windowB.onReceivedStream(function (stream) {
    video.srcObject = stream;
});

API

Class: WindowPeerConnection

Parameter

  • windowName: the BrowserWindow’s name in main process

The WindowPeeerConnection wraps around webkitRTCPeerConnection class, which on construction in a renderer process, sets up a IPC listeners that receive message and data from other windows. All the message are relayed through the main process, and data are serialized before transmitted.

Method

  • attachStream ( streamToSend ) : attaches a MediaStream object on the peer connection channel, which can be transmitted by sendStream method. Currently, it can only attach one stream per connection.
  • removeStream ( ) : removes the MediaStream attached to this peer connection channel.
  • sendStream ( receiverName ) : sends the attached MediaStream object to the target window specified the string ‘receiverName’. receiverName refers to the name of the variable assigned to the target BrowserWindow in the Electron’s main process.

Event Listener

  • onReceivedStream ( function(receivedStream) ) : triggered when the WindowPeerConnection receives a MediaStream from a remote window.

Main Process: Data Relay Channel

A Javascript object that is called in the Electron’s main process. Since Electron does not support direct IPC between BrowserWindows, all messages and data must be relayed through the main process, using ipcMain. Each BrowserWindow is wrapped as a client in the data Channel.

Function

  • addClient ( client ) : adds a client on the data relay channel. A client is a Javascript object with the following format: client = { window: browerWindowObject, name: "BrowserWindowName" }

  • removeClient ( clientName ) : removes a client with the given window name from the data relay channel.

  • initChannel ( ) : initiates the data relay channel with the current array of clients. Clients can be furthered added after the channel is initiated.

  • dispose ( ) : disposes the data relay channel.

License

MIT

1.0.6

7 years ago

1.0.5

7 years ago

0.2.1

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago