Skip to content

Commit

Permalink
Add React media components (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuaYoo authored and hugooliveirad committed Nov 23, 2020
1 parent 21265ba commit 42a5278
Show file tree
Hide file tree
Showing 13 changed files with 1,738 additions and 13,248 deletions.
115 changes: 100 additions & 15 deletions packages/react-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function App() {

```jsx
// Phone.jsx
import { useNotification } from '@telnyx/react-client';
import { useNotification, Audio } from '@telnyx/react-client';

function Phone() {
const notification = useNotification();
Expand All @@ -42,12 +42,16 @@ function Phone() {
{activeCall &&
activeCall.state === 'ringing' &&
'You have an incoming call.'}

<Audio stream={activeCall && activeCall.remoteStream} />
</div>
);
}
```

### with `useCallbacks`
### Hooks

#### `useCallbacks`

```jsx
import { useCallbacks } from '@telnyx/react-client';
Expand All @@ -56,14 +60,84 @@ function Phone() {
useCallbacks({
onReady: () => console.log('client ready'),
onError: () => console.log('client registration error'),
onSocketError: () => console.log('client socket error'),
onSocketClose: () => console.log('client disconnected'),
onNotification: (x) => console.log('received notification:', x),
});

// ...
}
```

### with `TelnyxRTCContext.Consumer`
#### `useTelnyxRTC`

If you need more fine-tuned control over TelnyxRTC, you also have access to `useTelnyxRTC` directly.

```jsx
import { useTelnyxRTC } from '@telnyx/react-client';

function Phone() {
const client = useTelnyxRTC({ login_token: 'mytoken' });

client.on('telnyx.ready', () => {
console.log('client ready');
});

// ...
}
```

Take care to use this hook only once in your application. For most cases, we recommend you use [TelnyxRTCContext/TelnyxRTCProvider](#TelnyxRTCContextProvider) instead of this hook directly. This ensures that you only have one Telnyx client instance running at a time.

#### `useContext` with `TelnyxRTCContext`

You can retrieve the current TelnyxRTC context value by using React's [`useContext` hook](https://reactjs.org/docs/hooks-reference.html#usecontext), as an alternative to [TelnyxRTCContext.Consumer](#TelnyxRTCContextConsumer).

```jsx
import React, { useContext } from 'react';
import { TelnyxRTCContext } from '@telnyx/react-client';

function Phone() {
const client = useContext(TelnyxRTCContext);

client.on('telnyx.ready', () => {
console.log('client ready');
});

// ...
}
```

### Components

#### `TelnyxRTCContextProvider`

```jsx
import { TelnyxRTCProvider } from '@telnyx/react-client';

function App() {
const credential = {
// You can either use your On-Demand Credential token
// or your Telnyx SIP username and password
// login_token: 'mytoken',
login: 'myusername',
password: 'mypassword',
};

const options = {
ringtoneFile: 'https://example.com/sounds/incoming_call.mp3',
ringbackFile: 'https://example.com/sounds/ringback_tone.mp3',
};

return (
<TelnyxRTCProvider credential={credential} options={options}>
<Phone />
</TelnyxRTCProvider>
);
}
```

#### `TelnyxRTCContext.Consumer`

```jsx
import { TelnyxRTCContext } from '@telnyx/react-client';
Expand All @@ -77,39 +151,50 @@ function PhoneWrapper() {
}
```

### `useTelnyxRTC` usage
#### `Audio`

```jsx
import { useTelnyxRTC } from '@telnyx/react-client';
import { Audio } from '@telnyx/react-client';

function Phone() {
const client = useTelnyxRTC({ login_token: 'mytoken' });
function Phone({ activeCall }) {
return (
<div>
<Audio stream={activeCall.remoteStream} />
</div>
);
}
```

client.on('telnyx.ready', () => {
console.log('client ready');
});
#### `Video`

// ...
```jsx
import { Video } from '@telnyx/react-client';

function VideoConference({ activeCall }) {
return (
<div>
<Video stream={activeCall.localStream} />
<Video stream={activeCall.remoteStream} />
</div>
);
}
```

You should only have one Telnyx client instance running at a time. To ensure a single instance, it's recommended to use `TelnyxRTCContext`/`TelnyxRTCProvider` instead of using this hook directly.

## Development

Install dependencies:

```bash
npm install
# in another tab:
cd example && npm install
cd example && npm install && npm run setup
```

Watch and compile files:

```bash
npm start
# in another tab:
# fill in example/.env, then in another tab:
cd example && npm start
```

Expand Down
2 changes: 2 additions & 0 deletions packages/react-client/example/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REACT_APP_TELNYX_LOGIN_TOKEN=
REACT_APP_TELNYX_PHONE_NUMBER=
9 changes: 8 additions & 1 deletion packages/react-client/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ This example was bootstrapped with [Create React App](https://github.com/faceboo

It is linked to the @telnyx/react-client package in the parent directory for development purposes.

You can run `npm install` and then `npm start` to test your package.
To install the example app:

```shell
npm install
npm run setup
```

To start the app, fill in `.env` with information from your Telnyx portal and run `npm start`.
Loading

0 comments on commit 42a5278

Please sign in to comment.