Janus Gateway Client
npm install janus-gateway-client
yarn add janus-gateway-client
`
!alt text
Usage
`
import { JanusClient } from 'janus-gateway-client';
import ReconnectingWebSocket from 'reconnecting-websocket';
...
const client = new JanusClient({
onPublisher: this.onPublisher,
onSubscriber: this.onSubscriber,
onError: (error) => console.error(error),
user_id,
server,
logger: this.logger,
WebSocket: ReconnectingWebSocket,
subscriberRtcConfiguration: rtcConfiguration,
publisherRtcConfiguration: rtcConfiguration,
transactionTimeout: 15000,
keepAliveInterval: 10000
});
this.client.initialize()
.then(() => (
this.client.getRooms()
))
.then(({ load }) => {
const rooms = load; //use one of the rooms objects to retrieve room id and join specific room
this.connected = true;
});
`
Options
$3
> string | _required_
server running janus-gateway-node.
`
const server = "wss://yoururl.com";
`
$3
> (subscriber:JanusSubscriber) => void | _required_
this function will notify user when new participant joined room in which user currently publishing media.
`
const onSubscriber = async (subscriber) => {
subscriber.addEventListener("terminated", this.onSubscriberTerminated(subscriber));
subscriber.addEventListener("leaving", this.onSubscriberLeaving(subscriber));
subscriber.addEventListener("disconnected", this.onSubscriberLeaving(subscriber));
try {
await subscriber.initialize();
if (this.props.onParticipantJoined) {
this.props.onParticipantJoined(subscriber);
}
const subscribers = this.getSubscribers();
if (this.nParticipants!==subscribers.length) {
this.nParticipants = subscribers.length;
this.onParticipantsAmountChange();
}
this.forceUpdate();
} catch(error) {
this.props.onError(error);
}
}
`
$3
> (publisher:JanusPublisher) => void | _required_
called after publisher succesfully joined room.
`
const onPublisher = async (publisher) => {
publisher.addEventListener("terminated", this.onPublisherTerminated(publisher));
publisher.addEventListener("disconnected", this.onPublisherDisconnected(publisher));
if (this.props.onConnected) {
this.props.onConnected(publisher);
}
this.forceUpdate();
}
`
$3
> any | _required_
websocket instance.
$3
> number | _required_
time interval before incomplete transaction will throw timeout error.
$3
> number | _required_
keepalive time interval for user.
$3
> user_id | _required_
unique user identifier.
$3
> (error:any) => void | _required_
in case error occurred this. function will be invoked to notify user about error.
$3
> Logger | _required_
customize logging
`
interface Logger {
enable: () => void,
disable: () => void,
success: (...args:any[]) => void,
info: (...args:any[]) => void,
error: (error:any) => void,
json: (...args:any[]) => void,
tag: (tag:string, type:success | info | error) => (...args:any[]) => void
}
`
$3
> any | optional
$3
> any | optional
Instance methods
$3
> () => Promise
establish connection with the server.
`
...
await this.client.initialize();
`
$3
> () => Promise
terminate connection with server, perform cleanup actions.
`
...
await this.client.terminate();
`
$3
> (cameraDeviceId:string) => Promise
replace current video source.
`
...
await this.client.replaceVideoTrack(this.props.cameraId);
`
$3
> (room_id:string, mediaConstraints?: MediaStreamConstraints) => Promise
join specific room as a publisher and subscribe to all available participants feeds.
`
...
await this.client.join(room_id, mediaConstraints);
`
$3
> () => Promise
leave room in which user currently resides.
`
...
await this.client.leave();
``