Certain dependencies of janus.js
may be passed during library initialization as a property list containing the following keys:
newWebSocket:
a function which given WebSockets server and protocol arguments should return a new WebSocket (or something that acts like it)webRTCAdapter:
an adapter
object such as provided by the webrtc-adapter libraryisArray:
a function which tests if a given argument is a JavaScript arraycheckJanusExtension:
a function which tests if the Janus Screensharing extension for Chrome is installed/available. This can be done by testing whether or not an element with an id
attribute value of janus-extension-installed
is present.httpAPICall:
a function which given an url and options argument performs an HTTP API request to Janus. This function is not as straightforward to implement, see the section on httpAPICall below for details.Depending on your needs you do not have to provide all these dependencies, e.g. you do not need to implement the httpAPICall
function if your application relies exclusively on WebSockets to access the Janus API.
Two implementations of the dependencies object are provided by janus.js:
Janus.useDefaultDependencies
Janus.useOldDependencies
In turn, each of these implementations accept their dependencies as arguments or fallback on certain global variables. Below follows an overview:
The Janus.useDefaultDependencies
method relies on the following native browser APIs:
Promise:
support for Promises
as standardised in ES 6 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises)fetch:
support for the fetch
API (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)WebSocket:
support for the WebSocket
API (https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)document.querySelector:
support for the document.querySelector
API (https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector)Additionally the adapter
object from the webrtc-adapter library is also required. These dependencies may either be passed explicitly to the function as a property list with keys of the same name, or if omitted the function will fallback to relying on global variables of that name instead.
Example:
var customDependencies = Janus.useDefaultDependencies({ fetch: myCustomFetchImplementation // myCustomFetchImplementation should provide a compatible fetch() API }); var relyingOnGlobalsEntirely = Janus.useDefaultDependencies();
Being able to passe dependencies like this is especially useful in the context of modern ES modules:
import adapter from 'webrtc-adapter'; // other imports elided const setupDeps = () => Janus.useDefaultDependencies({ adapter, // other dependencies elided }); export const initialiseJanusLibrary = () => Janus.init({dependencies: setupDeps()});
The Janus.useOldDependencies
method relies on:
jQuery:
the JQuery library (http://jquery.com/)WebSocket:
support for the WebSocket
API (https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)adapter:
the adapter
object from the webrtc-adapter libraryThis function provides a simple upgrade path for existing applications which are heavily tied to jQuery (especially since previous versions of janus.js
depended on it).
The httpAPICall
function is used to issue API calls to the Janus HTTP(S) interfaces. It will be passed two arguments:
url:
a string which refers to the (server) URL of the API endpoint to contactoptions
a property list (see below)Any return values from the httpAPICall
function will be ignored.
When working with HTTP request or response bodies, the httpAPICall
is responsible for serialisation to, and deserialisation from the 'wire format' (JSON). That is: the httpAPICall
must transform objects to JSON or parse JSON as and when required. Similarly, the httpAPICall
is also responsible for setting appropriate HTTP Content-Type
(application/json) and/or Accept
headers.
The options
argument may contain the following keys:
timeout:
a timeout in milliseconds which should be imposed on the request. The httpAPICall
implementation is required to implement support for imposing timeouts on HTTP API requests.body:
payload to include as body of the outgoing HTTP request. The httpAPICall
must encode it in the 'wire format' (JSON).withCredentials:
a boolean indicating whether or not HTTP credentials should be sentsuccess:
a callback which should be dispatched when an API request was successful.error:
a callback which should be dispatched when an API request was unsuccessful, or timed outasync:
a boolean hint which indicates whether or not asynchronous requests are desirable. This hint is a primarily a remnant for backwards compatible behaviour when working with jQuery.The success
callback should be passed the deserialised API response body. The error
callback accepts two arguments: a descriptive status text string and the raw error object which caused the error
callback to be invoked.
httpAPICall
represents the primary way to intercept HTTP(S) API calls issued from within the janus.js
library. You can use this mechanism to augment outgoing requests with additional headers or to intercept responses. For example:Authorization
header by injecting it into outgoing API requests and routing them through a proxy.httpAPICall
as an action creator in your Redux application.To use a different extension for screensharing permissions in Chrome you can pass a extension
object to Janus.useDefaultDependencies
and Janus.useOldDependencies
. The object should provide the following methods:
init()
: Do any setup work here. Will be called once when the dependencies are loaded.isInstalled()
: should return a boolean indicating whether the Extension was detected and is ready to use.getScreen(callback)
: make a call to the extension to get a streamId
here. The streamId can be obtained from chrome using chrome.desktopCapture.chooseDesktopMedia(). When the request is successful pass the streamId
back using callback(null, streamId)
, otherwise pass an Error
object like callback(error)