0.3.8 • Published 3 years ago

webrtc-fw v0.3.8

Weekly downloads
23
License
Apache-2.0
Repository
github
Last release
3 years ago

WebRTC framework

Build Coverage Status

A simple WebRTC client-side framework.

How to install

npm i webrtc-fw

How to use

  1. Implement signalling server communication layer wrapped into provided SignallingBuilder.
  2. Create new instance of WebRTC object with the signalling API.
  3. Initialize WebRTC connection with the prepare method.
import { WebRTC, SignallingBuilder, Socket } from 'webrtc-fw';

// [1]
// a signalling server API implementation
//
// example Websocket server communication
//
function factory({
  url = 'ws://localhost',
  onConnect,
  onClose,
  onError,
  onServerError,
  onMessage,
  onOffer,
  onOpen,
} = {}) {
  /** @type {WebSocket} */
  let connection;

  const onMessageMiddleware = function (message) {
    const { data } = message;

    if (data === 'HELLO') {
        console.info(`[signalling] session is opened`);
    } else if (data === 'ERROR') {
        onServerError?.(data);
    } else if (data === 'OFFER') {
        onOffer?.();
    } else {
        onMessage?.(msg);
    }
  };

  const connect = function () {
    connection = Socket({
      address: url,
      onClose,
      onError,
      onMessage: onMessageMiddleware,
      onOpen,
    });

    onConnect(this);

    return connection;
  };

  const getUrl = () => url;

  const send = () => connection?.send(data);

  const close = () => connection?.close();

  const offerCandidate = (candidate) => send().encoded({ ice: candidate });

  const offerSession = (sdp) => send().encoded({ sdp });

  return Object.freeze({
    connect,
    close,
    getUrl,
    send,
    offerCandidate,
    offerSession,
  });
}

const api = () => SignallingBuilder({ factory });

// [2]
const rtc = WebRTC({
    api,
    onConnect: onServerConnect,
    onPrepare: onServerPrepare,
    onPrepareFail: () => console.error('Too many failed connection attempts, aborting'),
    onError: () => console.error("Couldn't connect to server"),
    onClose: onServerClose,
    onOpen: onServerOpen,
    onRemoteTrack,
  });

// [3]
// opens new WebRTC connection
rtc.prepare();
}
0.3.8

3 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago