Flex Manager
The Flex Manager is the access point for controlling your Flex instance and all of the underlying Twilio products used for communications and assigning tasks. This means that within your Flex project, you can access the TaskRouter or Chat client directly through the Flex manager.
Aside from Flex itself, Manager also gives you access to the Programmable Chat, Sync, Client, and TaskRouter SDKs.
Manager Class
See the official Flex UI 1.x docs on the Manager class for a full list of attributes and methods.
How to obtain the Manager instance
You can access the manager as follows:
By calling the `getInstance` method
Flex.Manager.getInstance()
By calling the `create` method when initializing Flex
return Flex
.provideLoginInfo(configuration, "#container")
.then(() => Flex.Manager.create(configuration))
.then(manager => {
// use manager here
})
.catch(error => handleError(error));
You can check out the sample project on how to initialize Flex.
In the init method of your plugin
init(flex, manager) {
// use manager here
}
Subscribing to Manager events
You can use Flex Manager to subscribe to events that occur from Flex. See Flex Events for more details.
import { Manager } from "@twilio/flex-ui";
const manager = Manager.getInstance();
manager.events.addListener('eventName', (payload) => {
// implement logic here
});
For example, you can subscribe to the pluginsLoaded
event to know when all Flex Plugins have loaded.
manager.events.addListener("pluginsLoaded", () => {
console.log("Plugins have been loaded!");
});
Common use cases and examples
This example logs connect
in the browser’s console whenever the agent connects to a call:
Flex.Manager.getInstance().voiceClient.on('connect', () => {
console.log('connect');
});
By mixing calls to the Manager with the Actions Framework, you can perform more complex tasks like this example that automatically accepts all inbound chats for agents:
Flex.Manager.getInstance().workerClient.on("reservationCreated", reservation => {
if (reservation.task.taskChannelUniqueName === 'chat' && reservation.task.direction === 'inbound') {
Flex.Actions.invokeAction("AcceptTask", {sid: reservation.sid});
Flex.Actions.invokeAction("SelectTask", {sid: reservation.sid});
}
});
insightsClient
The insightsClient
provide access to the Twilio Sync SDK. For Flex accounts, this gives access to workers and tasks data through the use of two classes:
- LiveQuery class: to query Flex data and receives pushed updates whenever new (or updated) records would match the given expression
- InstantQuery class: to get a static snapshot of Flex data
Both classes needs two arguments:
- Index name: data set the query is executed against. Currently supported index names for Flex are:
tr-task
,tr-worker
,tr-reservation
,tr-queue
. - Query: this is the query used to filter the data from the index. The syntax for the query is documented here. The query can be an empty string: in this case the whole data set is returned (e.g. all workers.)
LiveQuery example
In this example, the insightsClient is used to query the workers with activity_name
set to Available
and subscribe to changes. That means that every time a worker change its status to Available
, an event itemUpdated
is fired. If a worker changes its status from Available
to any other status, the itemRemoved
event is fired.
Flex.Manager.insightsClient
.liveQuery('tr-worker', 'data.activity_name == "Available"')
.then(function (args) {
console.log(
'Subscribed to live data updates for worker in "Available" activity'
);
args.on('itemRemoved', function (args) {
console.log('Worker ' + args.key + ' is no longer "Available"');
});
args.on('itemUpdated', function (args) {
console.log('Worker ' + args.key + ' is now "Available"');
});
})
.catch(function (err) {
console.log('Error when subscribing to live updates', err);
});
InstantQuery example
In this example, the insightsClient is used to query the workers with specific skills inside its attributes
. This returns an array of workers that can be used to provide static data.
manager.insightsClient.instantQuery('tr-worker').then((q) => {
q.on('searchResult', (items) => {
// Do something with the results
});
q.search('data.attributes.languages contains "english"');
});
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.