Giter Club home page Giter Club logo

webrtc-fw's Introduction

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();
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.