A small Javascript wrapper for talking to the Janus WebRTC signalling API.
Перейти к файлу
Marshall Polaris cf259aee5d Clean up CoC 2019-04-01 18:17:44 -07:00
.gitignore Initial commit 2017-10-20 16:21:33 -07:00
.travis.yml Specify yarn-compatible Node version in travis.yml 2017-10-21 01:05:18 -07:00
CODE_OF_CONDUCT.md Clean up CoC 2019-04-01 18:17:44 -07:00
LICENSE Initial commit 2017-10-20 16:21:33 -07:00
README.md Bump deps, prefer npm over yarn 2018-06-19 14:13:55 -07:00
bundle.js Bump deps, prefer npm over yarn 2018-06-19 14:13:55 -07:00
minijanus.js Make log messages way nicer to read 2018-05-02 19:48:09 -07:00
package-lock.json 0.6.2 2018-06-19 14:14:34 -07:00
package.json 0.6.2 2018-06-19 14:14:34 -07:00
tests.js More work related to session disposal 2018-04-17 18:22:08 -07:00

README.md

minijanus.js

npm Build Status

A super-simplistic and -minimal wrapper for talking to the Janus signalling API. Developed for use with Janus as a web game networking backend via janus-plugin-sfu, but fundamentally plugin-agnostic. Designed to provide useful possible abstractions while still providing the maximum possible control over RTCPeerConnection configuration and precise plugin signalling flow.

If you want a batteries-included wrapper, you should use the one distributed by the Janus developers -- janus.js. This one is different in a few ways:

  1. It doesn't try to maintain compatibility with older browsers very hard; the use case is modern browsers only.
  2. It's very small and straightforward, so it may serve as a useful reference client for people who want to better understand the signalling API.
  3. It gives you control over most of the configuration and usage of the RTCPeerConnection directly, whereas janus.js wraps and manages the connection for you.

If you want a similar but moderately more featureful wrapper, check out minnie-janus.

Example

Require minijanus in Node, or link to bundle.js in a browser. Then:

var ws = new WebSocket("ws://localhost:8188", "janus-protocol");
var session = new JanusPluginSession(ws.send.bind(ws));
var handle = new JanusPluginHandle(session);
var conn = new RTCPeerConnection({});

ws.addEventListener("message", ev => session.receive(JSON.parse(ev.data)));
ws.addEventListener("open", _ => {
  session.create()
    .then(_ => handle.attach("janus.plugin.sfu"))
    .then(_ => {
      conn.addEventListener("icecandidate", ev => {
        handle.sendTrickle(ev.candidate || null).catch(e => console.error("Error trickling ICE: ", e));
      });
      conn.addEventListener("negotiationneeded", _ => {
        var offer = conn.createOffer();
        var local = offer.then(o => conn.setLocalDescription(o));
        var remote = offer.then(j => handle.sendJsep(j)).then(r => conn.setRemoteDescription(r.jsep));
        Promise.all([local, remote]).catch(e => console.error("Error negotiating offer: ", e));
      });
      var unreliableCh = conn.createDataChannel("unreliable", { ordered: false, maxRetransmits: 0 });
      var reliableCh = conn.createDataChannel("reliable", { ordered: true });
      navigator.mediaDevices.getUserMedia({ audio: true })
        .then(m => m.getTracks().forEach(t => conn.addTrack(t, m)))
        .catch(e => console.error("Error acquiring media: ", e));
      return new Promise(resolve => handle.on("webrtcup", resolve));
    })
    .then(_ => { console.info("Connected to Janus: ", conn); })
    .catch(e => { console.error("Error connecting to Janus: ", e); });
});

(Note that this example code first negotiates only the data channels, and then renegotiates afterward when the microphone permission is provided. Only recent versions of Janus support renegotiation. If you didn't want this, you would instead wait to create the connection until the microphone permission was granted.)

Building

To generate bundle.js:

$ npm run build

Testing

$ npm test