Twilio Client Redux
Purpose
This package is intended to be used with twilio-client.js
and to make integrating its event emitters with redux very straightforward. We recommend using it along side Redux Toolkit for several reasons and all code examples are shown with RTK implementations.
The general implementation of this library is currently very opinionated, but should work for every use case - if you have any specific requests, please open an issue or PR and we'd be happy to include it. Our goal is to track twilio-client
releases and upgrade as necessary.
Note: As of right now, Twilio does not export types that are easily consumable in their frontend libraries, but we do our best to give you an accurate representation of the serializable device and connection when appropriate. This is mostly just for logging purposes as you can't call methods on the serialized
Device
orConnection
objects.
Installation
Using npm
:
npm i twilio-client-redux
Using yarn
:
yarn add twilio-client-redux
Usage
Add the middleware
At a high level, the implementation looks like this:
- Add the middleware
- Create a device instance with your capability token by
dispatch
ingsetup(token, options)
dispatch
actions to make calls, control the device, etc etc.- Respond to events in relevant reducers to handle
Device
and/orConnection
events - Have a phone party!
The middleware accepts the following options:
Example implementation:
// store.ts ;; ; ; ; ;
Initialize a device
// MyPhoneApp.ts;;; ; return PhoneUI /; };
Device setup options:
Accepts the standard twilio-client
Device
options:
declare
Device actions:
Note: All devices actions take an optional parameter of
deviceId
. By default, this is set to 'default'. This allows you to manage multiple devices if your implementation requires that.
setup; // initialize a device.destroy; // destroy the device instance and disconnect all connections. you will need to call setup again to create a new device.setOutputDevice; // if you passed in storeAudioDevices: true to `setup`, this will store in localStorage. it will attempt to be reused when the device initialized in the futuresetInputDevice; // sets the input device and behaves the same as setOutputDevicetestOutputDevice; // can be used to play a chime on the currently set output device
Managing connections
We provide every method that is available to twilio-client.js
as exported actions. All methods take an optional deviceId
parameter.
makeCall;hangupCall;sendDigit; // sends a digit to the active connection setMute; // mute the current connection on the devicetoggleMute; // toggles the mute stategetMuteStatus; // returns `{ muted: value }` getStatus; // returns the status of the active connection acceptCall;rejectCall;ignoreCall;
Responding to middleware-generated actions in a reducer
By default, the middleware creates actions for every twilio-client
event listener. Depending on your redux setup, you can append .type
to the end of the action you're looking for to get it's type value (ex: onReady.type
=== @tcr::DEVICE_READY
).
In a non-createSlice
implementation, that might look like:
// phoneReducer.js// ... reducer code
Included action types that are created after initializing a Device
onReady;onCancel;onConnect;onError;onDisconnect;onIncoming;onOffline;
Example slice responding to common use cases:
// phoneSlice.ts ;;;; // in this case, we make an API request to our server to initiate a call. being that// the default value in the middleware is to automatically accept incoming connections// our call will be bridged immediately. your implementation will vary.; ; ; ; ;