# react-js-sdk

[![npm version][npm-image]][npm-url]

This document covers the 555 JavaScript SDK's React Component.

## Getting started

`$ npm install iris-react-sdk --save`

## Usage - Examples

```javascript
import {RoomContainer, RtcSdk} from 'iris-react-sdk';
```

1. Make connection using `iristoken` and `routingId`
  	```javascript

    // Call connection method
    RtcSdk.connect(iristoken, routingId, config.urls.eventManager);

    //Listen to onConnected event
    RtcSdk.onConnected = () => {
      console.log("App :: onConnected :: Iris connection successful");
    }

    //Listen to notification event
    RtcSdk.onNotification = (payload) => {
      console.log("App:: onNotification :: Payload : ", JSON.stringify(payload));
    }

    //Listen to iris diconnected event
    RtcSdk.onDisconnected = () => {
      console.log("App:: onDisconnected");
    }


    //Listen to connection errors
    RtcSdk.onError = (error) => {
      console.log("App:: onError :: error : ", error);
    }

    ```

2. Using `RoomContainer` to initiate/accept call

```js

return (
  <RoomContainer
  ref="room"
  Type={this.state.Type}
  RoomId={this.state.RoomId}
  Config={this.state.Config}
  NotificationPayload={this.state.NotificationPayload}
  onLocalStream={this.onLocalStream}
  onRemoteStream={this.onRemoteStream}
  onSessionCreated={this.onSessionCreated}
  onSessionJoined={this.onSessionJoined}
  onSessionConnected={this.onSessionConnected}
  onSessionParticipantJoined={this.onSessionParticipantJoined}
  onSessionParticipantAudioMuted={this.onSessionParticipantAudioMuted}  
  onSessionParticipantLeft={this.onSessionParticipantLeft}
  onSessionEnd={this.onSessionEnd}
  onUserProfileChange={this.onUserProfileChange}
  onSessionSIPStatus={this.onSessionSIPStatus}
  onError={this.onError}
  onEvent={this.onEvent}
  />
)

...

let roomId = response.room_id;
let Config = {
  irisToken: "",
  routingId: "",
  toTN: "",
  fromTN: "",
  toRoutingId: "",
  traceId: "",
  userData: "",
  SessionType: 'outgoing',
  notificationPayload: ''
};
this.setState({
  RoomId:roomId,
  Config:Config,
});

```

# APIs

## **Connection APIs - Make a connection using `connect`**
* **Example**

```js
import RtcSdk from 'iris-react-sdk';
RtcSdk.connect(irisToken, routingId, serverUrl);
```
* **Params**

  * `serverUrl` **{String}**: The url to event manager
  * `irisToken` **{String}**: A valid IRIS token
  * `routingId` **{String}**: Routing id of the user who is trying to login


## **Connection APIs - Disconnect using `disconnect`**
* **Example**

```js
import RtcSdk from 'iris-react-sdk';
RtcSdk.disconnect();
```
* **Params**

  * None



## **Media devices - Get a list of meida devices with `getMediaDevices`**

Returns a promise with list of available audio and video devies

* **Example**

```js
import RtcSdk from 'iris-react-sdk';
RtcSdk.getMediaDevices();
```

## **Request Media Device Permission - `requestMediaPermission`**

Returns a promise with boolean whether permission is acquired or not

* **Example**

```js
import RtcSdk from 'iris-react-sdk';
let deviceType = 'microphone'
RtcSdk.requestMediaPermission(deviceType).then((permissionAcquired){
  if(permissionAcquired){
    // Success
  }else{
    // Failed
  }
});
```


## **PSTN APIs - Hold a PSTN call using `pstnHold`**

* **Example**

```js
this.refs.room.pstnHold(roomId, participantJid);
```
* **Params**

  * `roomId` **{String}** : Room Id
  * `participantJid` **{String}**: Remote participant routingId/resourceId



## **PSTN APIs - UnHold a PSTN call using `pstnUnHold`**

* **Example**

```js
this.refs.room.pstnUnHold(roomId, participantJid);
```
* **Params**

  * `roomId` **{String}** : Room Id
  * `participantJid` **{String}**: Remote participant routingId/resourceId



## **PSTN APIs - Merge two PSTN calls using `pstnMerge`**

* **Example**

```js
this.refs.room.pstnMerge(roomId, firstParticipantJid, secondParticipantJid);
```
* **Params**

  * `roomId` **{String}** : Room Id
  * `firstParticipantJid` **{String}**: First remote participant's routingId/resourceId
  * `secondSession` **{object}**: IrisRtcSession object of the session to be merged
  * `secondParticipantJid` **{String}**: Second remote participant's routingId/resourceId



## **PSTN APIs - End PSTN call using `pstnHangup`**

* **Example**

```js
this.refs.room.pstnHangup(roomId, participantJid);
```
* **Params**

  * `roomId` **{String}** : Room Id
  * `participantJid` **{String}**: Remote participant's routingId/resourceId



<br/><br/>
# Callbacks

## **Connection - For connection successful listen to  `onConnected`**

* **Example**

```js
RtcSdk.onConnected = () => {
  console.log("App :: onConnected :: Iris connection successful");
}
```

* **Params**

  * None


## **Connection - For Notification listen to  `onNotification`**

* **Example**

```js
RtcSdk.onNotification = (notificationPayload) => {
  console.log("App :: notificationPayload :: Iris notification received");
}
```

* **Params**

  * `notificationPayload` **{json}** : Notification payload with the information to join session


## **Session - For PSTN SIP Status listen to  `onSessionSIPStatus`**

* **Example**

```js
  onSessionSIPStatus = (roomId, participantJid, status) => {
  console.log("App :: onSessionSIPStatus :: PSTN SIP Status : "+ status);
}
```

* **Params**

  * `roomId` **{String}** : RoomId of the session
  * `participantJid` **{String}** : Jid of the participant
  * `status` **{String}** : PSTN Status ex. initializing, connecting, ringing, connected.


## **RoomContainer - For local media stream `onLocalStream`**

* **Example**

```js
onLocalStream = (stream) => {
  console.log("App :: onLocalStream :: Local media stream received");
}
```

* **Params**

  * `stream` **{object}** : Local media stream



[npm-image]: https://flat.badgen.net/npm/v/iris-react-sdk
[npm-url]: https://www.npmjs.com/package/iris-react-sdk
