Rate this page:

Control Worker Activities using Worker.js: Add an Agent UI to our Project

Let's get started on our agent UI. Assuming you've followed the conventions so far in this tutorial, the UI we create will be accessible using your web browser at:

http://localhost:8080/agent.php?WorkerSid=WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX (substitute your Alice's WorkerSid)

We pass the WorkerSid in the URL to avoid implementing complex user management in our demo. In reality, you are likely to store a user's WorkerSid in your database alongside other User attributes.

First, create a file called agent.php and add the following code. Remember to substitute your own account and Workspace credentials between the curly braces:

Loading Code Sample...


        You'll notice that we included three external files:

        • taskrouter.min.js is the primary TaskRouter.js JavaScript file that communicates with TaskRouter's infrastructure on our behalf. You can use this URL to include TaskRouter.js in your production application, but first check the reference documentation to ensure that you include the latest version number.
        • agent.css is a simple CSS file created for the purpose of this Quickstart. It saves us having to type out some simple pre-defined styles.
        • agent.js contains our custom JavaScript logic for this application. We need to create that next, but first...

        Download and extract Twilio's PHP helper library into the same directory that you are working in. This is required as our code references twilio-php/Services/Twilio.php. For more advanced PHP users, the composer package manager will also work fine. See the twilio-php documentation.

        Create the agent.js file in the same directory as your agent.php and add the following code:


        /* Subscribe to a subset of the available Worker.js events */
        function registerTaskRouterCallbacks() {
          worker.on('ready', function(worker) {
              logger("Successfully registered as: " + worker.friendlyName)
              logger("Current activity is: " + worker.activityName);
          worker.on('activity.update', function(worker) {
              logger("Worker activity changed to: " + worker.activityName);
          worker.on("reservation.created", function(reservation) {
            logger("You have been reserved to handle a call!");
            logger("Call from: " + reservation.task.attributes.from);
            logger("Selected language: " + reservation.task.attributes.selected_language);
          worker.on("reservation.accepted", function(reservation) {
            logger("Reservation " + reservation.sid + " accepted!");
          worker.on("reservation.rejected", function(reservation) {
            logger("Reservation " + reservation.sid + " rejected!");
          worker.on("reservation.timeout", function(reservation) {
            logger("Reservation " + reservation.sid + " timed out!");
          worker.on("reservation.canceled", function(reservation) {
            logger("Reservation " + reservation.sid + " canceled!");
        /* Hook up the agent Activity buttons to Worker.js */
        function bindAgentActivityButtons() {
          // Fetch the full list of available Activities from TaskRouter. Store each
          // ActivitySid against the matching Friendly Name
          var activitySids = {};
          worker.activities.fetch(function(error, activityList) {
            var activities =;
            var i = activities.length;
            while (i--) {
              activitySids[activities[i].friendlyName] = activities[i].sid;
          /* For each button of class 'change-activity' in our Agent UI, look up the
          ActivitySid corresponding to the Friendly Name in the button’s next-activity
          data attribute. Use Worker.js to transition the agent to that ActivitySid
          when the button is clicked.*/
          var elements = document.getElementsByClassName('change-activity');
          var i = elements.length;
          while (i--) {
              elements[i].onclick = function() {
                var nextActivity = this.dataset.nextActivity;
                var nextActivitySid = activitySids[nextActivity];
        /* Update the UI to reflect a change in Activity */
        function agentActivityChanged(activity) {
        function hideAgentActivities() {
          var elements = document.getElementsByClassName('agent-activity');
          var i = elements.length;
          while (i--) {
              elements[i].style.display = 'none';
        function showAgentActivity(activity) {
          activity = activity.toLowerCase();
          var elements = document.getElementsByClassName(('agent-activity ' + activity));
          elements.item(0).style.display = 'block';
        /* Other stuff */
        function logger(message) {
          var log = document.getElementById('log');
          log.value += "\n> " + message;
          log.scrollTop = log.scrollHeight;
        window.onload = function() {
          // Initialize TaskRouter.js on page load using window.workerToken -
          // a Twilio Capability token that was set in a <script> in agent.php
          window.worker = new Twilio.TaskRouter.Worker(workerToken);

        And that's it! Open http://localhost:8080/agent.php?WorkerSid=WK012340123401234 in your browser and you should see the screen below. If you make the same phone call as we made in Part 3, you should see Alice's Activity transition on screen as she is reserved and assigned to handle the Task.

        If you see "Initializing..." and no progress, make sure that you have included the correct WorkerSid in the "WorkerSid" request parameter of the URL.

        For more details, refer to the TaskRouter.js Worker JavaScript SDK documentation.

        Completed Agent UI

        • This simple PoC has been tested in the latest version of popular browsers, including IE 11. *

        Rate this page:

        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.

        Loading Code Sample...

              Thank you for your feedback!

              Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

              Sending your feedback...
              🎉 Thank you for your feedback!
              Something went wrong. Please try again.

              Thanks for your feedback!