minijanus.js
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.
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:
- It doesn't try to maintain compatibility with older browsers very hard; the use case is modern browsers only.
- 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.
- 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.
Example
Require minijanus
in Node, or link to bundle.js in a browser. Then:
var ws = new WebSocket("ws://localhost:8188", "janus-protocol");
ws.addEventListener("open", () => {
var session = new Minijanus.JanusSession(ws.send.bind(ws));
ws.addEventListener("message", ev => session.receive(JSON.parse(ev.data)));
session.create().then(() => establishConnection(session)).then(() => {
console.info("Connection established: ", handle);
});
});
function negotiateIce(conn, handle) {
return new Promise((resolve, reject) => {
conn.addEventListener("icecandidate", ev => {
handle.sendTrickle(ev.candidate || null).then(() => {
if (!ev.candidate) {
resolve();
}
});
});
});
};
function establishConnection(session) {
var conn = new RTCPeerConnection({});
var handle = new Minijanus.JanusPluginHandle(session);
return handle.attach("janus.plugin.sfu").then(() => {
var iceReady = negotiateIce(conn, handle);
var unreliableCh = conn.createDataChannel("unreliable", { ordered: false, maxRetransmits: 0 });
var reliableCh = conn.createDataChannel("reliable", { ordered: true });
var mediaReady = navigator.mediaDevices.getUserMedia({ audio: true });
var offerReady = mediaReady
.then(media => {
conn.addStream(media);
return conn.createOffer({ audio: true });
}, () => conn.createOffer());
var localReady = offerReady.then(conn.setLocalDescription.bind(conn));
var remoteReady = offerReady
.then(handle.sendJsep.bind(handle))
.then(answer => conn.setRemoteDescription(answer.jsep));
return Promise.all([iceReady, localReady, remoteReady]);
});
}
Building
To generate bundle.js:
$ yarn build
Testing
$ yarn test