This version of the SDK is deprecated. Use taskrouter.js for integrating TaskRouter into front-end applications.
Want to learn how to use Worker.js to route tasks to a web-based application? Dive into the TaskRouter Quickstart.
TaskRouter's JavaScript SDK makes it easy to build web-based applications for the people that will process TaskRouter Tasks. The SDK allows developers to:
The TaskRouter JavaScript SDK makes a WebSocket connection to TaskRouter. TaskRouter events and commands, such as Task Assignment and Acceptance, or Activity changes, are communicated via this Websocket connection.
Include the TaskRouter Worker SDK in your JavaScript application as follows:
<script type="text/javascript" src="//media.twiliocdn.com/taskrouter/js/v1.0/taskrouter.worker.min.js"></script>
TaskRouter uses Twilio capability tokens to delegate scoped access to TaskRouter resources to your JavaScript application. Twilio capability tokens conform to the JSON Web Token (commonly referred to as a JWT and pronounced "jot") standard, which allow for limited-time use of credentials by a third party. Your web server needs to generate a Twilio capability token and provide it to your JavaScript application in order to register a TaskRouter worker.
There are three capabilities you can enable today (and in most cases you'll want to use all of them in your application):
Capability | Authorization |
---|---|
FetchAttributes | The endpoint can retrieve the registered Worker's attributes from the TaskRouter server. |
WorkerActivityUpdates | The endpoint can update the registered Worker's Activity. |
TaskReservationUpdates | The endpoint will receive an event whenever the Worker's Activity changes. |
You can generate a TaskRouter capability token using any of Twilio's Helper Libraries. You'll need to provide your Twilio AccountSid and AuthToken, along with the WorkspaceSid and WorkerSid for the Worker you would like to register. For example, using our PHP helper library you can create a token as follows:
$workerCapability = new Services_Twilio_TaskRouter_Worker_Capability($accountSid, $authToken, $workspaceSid, $workerSid);
You can then add capabilities to the token as follows:
1$workerCapability->allowWorkerFetchAttributes();2$workerCapability->allowWorkerActivityUpdates();3$workerCapability->allowTaskReservationUpdates();4$workerToken = $workerCapability->generateToken();
By default, tokens are good for one hour. Override this default timeout by specifying a new value (in seconds).
For example, to generate a token good for 8 hours:
$workerToken = $workerCapability->generateToken(28800); //60 * 60 * 8
Once you have generated a TaskRouter capability token, you can pass this to your front-end web application and initialize the JavaScript library as follows:
var worker = new Twilio.TaskRouter.Worker(WORKER_TOKEN);
The library will raise a 'ready' event once it has connected to TaskRouter:
1worker.on("ready", function(worker) {2console.log(worker.sid) // 'WKxxx'3console.log(worker.friendly_name) // 'Worker 1'4console.log(worker.activity_name) // 'Reserved'5console.log(worker.available) // false6});
See more about the methods and events exposed on this object below.
Worker.js exposes the following API:
Twilio.TaskRouter.Worker is the top-level class you'll use for managing a Worker's activity, and receiving notifications when a Worker is assigned a task or when a Worker's Activity is changed.
Register a new Twilio.TaskRouter.Worker with the capabilities provided in workerToken
.
Name | Type | Description |
---|---|---|
workerToken | String | A Twilio TaskRouter capability token. See Creating a TaskRouter capability token for more information. |
var worker = new Twilio.TaskRouter.Worker(WORKER_TOKEN);
Updates the Worker's activity state. Note that when the is successful, the worker.activity.update
event will also fire.
Name | Type | Description |
---|---|---|
activitySid | String | The ActivitySid identifying the Activity you would like to set for the Worker |
resultCallback | Function | (optional) A JavaScript Function that will be called with the result of the update. If an error occurs, the first argument passed to this function will be an Error. If the update is successful, the first argument will be null and the second argument will contain the updated Worker object. |
1worker.updateActivity("WAxxx", function(error, worker) {2if(error) {3console.log(error.code);4console.log(error.message);5} else {6console.log(worker.activity_name); // "Offline"7}8});
Updates the TaskRouter capability token for the Worker.
Name | Type | Description |
---|---|---|
workerToken | String | A valid TaskRouter capability token. |
1var token = refreshJWT(); // your method to retrieve a new capability token2worker.updateToken(token);
Retrieves the list of Activities configured in your TaskRouter's Workspace.
Name | Type | Description |
---|---|---|
callback | Function | A function that will be called when the Activity list is returned. If an error occurs when retrieving the list, the first parameter passed to this function will contain the Error object. If the retrieval is successful, the first parameter will be null and the second parameter will contain an Array of Activities |
1worker.fetchActivityList(2function(error, activityList) {3if(error) {4console.log(error.code);5console.log(error.message);6return;7}8console.log("Parsing response");9var data = activityList.activities;10for(i=0; i<data.length; i++) {11console.log(data[i].friendly_name);12}13}14);
Attaches a listener to the specified event. See Events for the complete list of supported events.
Name | Type | Description |
---|---|---|
event | String | An event name. See Events for the complete list of supported events. |
callback | Function | A function that will be called when the specified Event is raised. |
1worker.on("worker.activity", function(worker) {2console.log(worker.activity_name) // 'Reserved'3console.log(worker.activity_sid) // 'WAxxx'4console.log(worker.available) // false5});
TaskRouter's JS library currently raises the following events to the registered Worker object:
The Worker has established a connection to TaskRouter and has completed initialization.
Name | Type | Description |
---|---|---|
worker | Worker | The Worker object for the Worker you've created. |
1worker.on("ready", function(worker) {2console.log(worker.available) // true3});
The Worker's activity has changed. This event is fired any time the Worker's Activity changes, both when TaskRouter updates a Worker's Activity, and when you make a change to the Worker's Activity via Worker.js or the TaskRouter REST API.
Name | Type | Description |
---|---|---|
worker | Worker | The updated Worker object |
1worker.on("activity.update", function(worker) {2console.log(worker.sid) // 'WKxxx'3console.log(worker.friendly_name) // 'Worker 1'4console.log(worker.activity_name) // 'Reserved'5console.log(worker.available) // false6});
The Worker's attributes have been updated.
Name | Type | Description |
---|---|---|
worker | Worker | The updated Worker object |
1worker.on("attributes.update", function(worker) {2console.log(worker.sid) // 'WKxxx'3console.log(worker.friendly_name) // 'Worker 1'4console.log(worker.activity_name) // 'Reserved'5console.log(worker.available) // false6});
The Worker has been assigned a Task.
Name | Type | Description |
---|---|---|
task | Task | The Task object that has been assigned to the Worker. |
Access Task and their attributes details as you would with any other JavaScript object.
1worker.on("reservation.created", function(task) {2console.log(task.attributes) // {foo: 'bar', baz: 'bang' }3console.log(task.priority) // 14console.log(task.age) // 3005console.log(task.tasksid) // WTxxx6console.log(task.reservation_sid) // WRxxx7});
Raised when the Worker has accepted a Task Reservation.
Name | Type | Description |
---|---|---|
worker | Worker | The updated Worker object. |
1worker.on("reservation.accepted", function(worker) {2console.log(worker.friendly_name) // Worker13console.log(worker.activity_name) // Busy4console.log(worker.available) // false5});
Raised when the Worker has rejected a Task Reservation
Name | Type | Description |
---|---|---|
worker | Worker | The updated Worker object. |
1worker.on("reservation.rejected", function(worker) {2console.log(worker.friendly_name) // Worker13console.log(worker.activity_name) // Offline4console.log(worker.available) // true5});
Raised when a pending Reservation associated with this Worker times out.
Name | Type | Description |
---|---|---|
task | Task | The Task object associated with the timed-out Reservation. |
1worker.on("reservation.timeout", function(task) {2console.log(task.attributes) // {foo: 'bar', baz: 'bang' }3console.log(task.priority) // 14console.log(task.age) // 3005console.log(task.tasksid) // WTxxx6console.log(task.reservation_sid) // WRxxx7});
Raised when a pending Reservation associated with this Worker is canceled.
Name | Type | Description |
---|---|---|
task | Task | The Task object associated with the timed-out Reservation. |
1worker.on("reservation.canceled", function(task) {2console.log(task.attributes) // {foo: 'bar', baz: 'bang' }3console.log(task.priority) // 14console.log(task.age) // 3005console.log(task.tasksid) // WTxxx6console.log(task.reservation_sid) // WRxxx7});
Raised when a pending Reservation associated with this Worker is rescinded in the case of multi-reservation.
Name | Type | Description |
---|---|---|
task | Task | The Task object associated with the timed-out Reservation. |
1worker.on("reservation.rescinded", function(task) {2console.log(task.attributes) // {foo: 'bar', baz: 'bang' }3console.log(task.priority) // 14console.log(task.age) // 3005console.log(task.tasksid) // WTxxx6console.log(task.reservation_sid) // WRxxx7});
Raised when the TaskRouter capability token used to create this Worker expires.
1worker.on("token.expired", function() {2console.log("updating token");3var token = refreshJWT(); // your method to retrieve a new capability token4worker.updateToken(token);5});