Chat with C# and ASP.NET MVC

Ready to implement a chat application using Twilio IP Messaging Client?

This application allow users to exchange messages through different channels, using the Twilio IP Messaging API. On this example, we'll show how to use this API capabilities to manage channels and their usages.

Properati built a web and mobile messaging app to help real estate buyers and sellers connect in real time. Learn more here.

Token Generation

In order to create a Twilio IP messaging client, you will need an access token. This token holds information about your Twilio Account and IP Messaging API keys.

We generate this token by creating a new Token and providing it with an IpMessagingGrant. With the Token at hand, we can use its method ToJwt() to return its string representation.

Loading Code Samples...
Language
using System.Collections.Generic;
using Twilio.Jwt.AccessToken;

namespace TwilioChat.Web.Domain
{
    public interface ITokenGenerator
    {
        string Generate(string identity, string endpointId);
    }

    public class TokenGenerator : ITokenGenerator
    {
        public string Generate(string identity, string endpointId)
        {
            var grants = new HashSet<IGrant>
            {
                new IpMessagingGrant {EndpointId = endpointId, ServiceSid = Configuration.IpmServiceSID}
            };

            var token = new Token(
                Configuration.AccountSID,
                Configuration.ApiKey,
                Configuration.ApiSecret,
                identity,
                grants: grants);

            return token.ToJwt();
        }
    }
}
TwilioChat.Web/Domain/TokenGenerator.cs
Generate an Access Token

TwilioChat.Web/Domain/TokenGenerator.cs

We can generate a token, now we need a way for the chat app to get it.

Token Generation Controller

On our controller we expose the endpoint responsible to provide a valid token. Using the information provided as parameters:

  • device: identifies the device being used.
  • identity: identifies the user itself.

It uses tokenGenerator.Generate method to get hold of a new token and return it in a JSON format to be used for our client.

Loading Code Samples...
Language
using System.Web.Mvc;
using TwilioChat.Web.Domain;

namespace TwilioChat.Web.Controllers
{
    public class TokenController : Controller
    {
        private readonly ITokenGenerator _tokenGenerator;

        public TokenController() : this(new TokenGenerator()) { }

        public TokenController(ITokenGenerator tokenGenerator)
        {
            _tokenGenerator = tokenGenerator;
        }

        // POST: Token
        [HttpPost]
        public ActionResult Index(string device, string identity)
        {
            if (device == null || identity == null) return null;

            const string appName = "TwilioChatDemo";
            var endpointId = string.Format("{0}:{1}:{2}", appName, identity, device);

            var token = _tokenGenerator.Generate(identity, endpointId);
            return Json(new {identity, token});
        }
    }
}
TwilioChat.Web/Controllers/TokenController.cs
Token Generation Controller

TwilioChat.Web/Controllers/TokenController.cs

Now that we have a route that generates JWT tokens on demand, let's use this route to initialize our Twilio IP Messaging Client.

Initialize the IP Messaging Client

On our client, we fetch a new Token using a POST request to our endpoint.

And with the token we can instantiate a new Twilio.AccessManager that is used to initialize our Twilio.IPMessaging.Client.

Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Initialize the IP Messaging Client

TwilioChat.Web/Scripts/twiliochat.js

Now that we've initialized our IP Messaging Client, lets see how we can get a list of channels.

Get the Channel List

After initializing the client, we can now call it's method getChannels to retrieve all visible channels. The method returns a promise as a result that we use to show the list of channels retrieved on the UI.

Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Get the Channel List

TwilioChat.Web/Scripts/twiliochat.js

Next, we need a default channel.

Join the General Channel

This application will try to join a channel called "General Channel" when it starts. If the channel doesn't exist, we'll create one with that name. The scope of this example application will show you how to work only with public channels, but the IP Messaging client allows you to create private channels and handle invitations.

Notice we set a unique name for the general channel as we don't want to create a new general channel every time we start the application.

Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Join the General Channel

TwilioChat.Web/Scripts/twiliochat.js

Now let's listen for some channel events.

Listen to Channel Events

With access to the channel objects we can use them to listen to a series of events. In our case, we're setting listeners to the following events:

  • messageAdded: When another member sends a message to the channel you are connected to.
  • typingStarted: When another member is typing a message on the channel that you are connected to.
  • typingEnded: When another member stops typing a message on the channel that you are connected to.
  • memberJoined: When another member joins the channel that you are connected to.
  • memberLeft: When another member leaves the channel that you are connected to.

Here, we just register a different function to handle each particular event.

Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Listen to Channel Events

TwilioChat.Web/Scripts/twiliochat.js

The client emits events as well. Let's see how we can listen to those events as well.

Listen to Client Events

Just like with channels, we can register handlers for events on the Client:

  • channelAdded: When a channel becomes visible to the Client.
  • channelRemoved: When a channel is no longer visible to the Client.
  • tokenExpired: When the supplied token expires.
Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Listen to Client Events

TwilioChat.Web/Scripts/twiliochat.js

We've actually got a real chat app going here, but let's make it more interesting with multiple channels.

Create a Channel

To create a new channel, the user clicks on the "+ Channel" link. That we'll show an input text field where it's possible to type the name of the new channel. The only restriction here, is that the user can't create a channel called "General Channel". Other than that, creating a channel is as simple as calling createChannel with an object that has the friendlyName key. You can create a channel with more options though, see a list of the options here.

Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Create a Channel

TwilioChat.Web/Scripts/twiliochat.js

Next, we will see how we can switch between channels.

Join Other Channels

When you tap on the name of a channel from the sidebar, that channel is set as the selectedChannel. The selectChannel method takes care of joining to the selected channel and setting up the selectedChannel.

Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Join Other Channels

TwilioChat.Web/Scripts/twiliochat.js

At some point your users will want to delete a channel. Let's have a look at how that can be done.

Delete a Channel

Deleting a channel is even more simple than creating one. The application lets the user delete the channel they are currently joined to through the "delete current channel" link. The only thing you need to do to actually delete the channel from Twilio, is call the delete method on the channel you are trying to delete. As other methods on the `Channel' object, It'll return a promise where you can set function that is going to handle successes.

Loading Code Samples...
Language
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
TwilioChat.Web/Scripts/twiliochat.js
Delete a Channel

TwilioChat.Web/Scripts/twiliochat.js

That's it! We've just implemented a simple chat application for C# using ASP.NET MVC.

Where to Next?

If you are a C# developer working with Twilio, you might want to check out these other tutorials:

SMS and MMS Notifications

Never miss another server outage. Learn how to build a server notification system that will alert all administrators via SMS when a server outage occurs.

Workflow Automation

Increase your rate of response by automating the workflows that are key to your business. In this tutorial, learn how to build a ready-for-scale automated SMS workflow, for a vacation rental company.

Masked Phone Numbers

Protect your users' privacy by anonymously connecting them with Twilio Voice and SMS. Learn how to create disposable phone numbers on-demand, so two users can communicate without exchanging personal information.

Did this help?

Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Tweet @twilio to let us know what you think.

Agustin Camino
David Prothero
Hector Ortega
Andrew Baker
Jose Oliveros

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 browsing the Twilio tag on Stack Overflow.

1 / 1
Loading Code Samples...
using System.Collections.Generic;
using Twilio.Jwt.AccessToken;

namespace TwilioChat.Web.Domain
{
    public interface ITokenGenerator
    {
        string Generate(string identity, string endpointId);
    }

    public class TokenGenerator : ITokenGenerator
    {
        public string Generate(string identity, string endpointId)
        {
            var grants = new HashSet<IGrant>
            {
                new IpMessagingGrant {EndpointId = endpointId, ServiceSid = Configuration.IpmServiceSID}
            };

            var token = new Token(
                Configuration.AccountSID,
                Configuration.ApiKey,
                Configuration.ApiSecret,
                identity,
                grants: grants);

            return token.ToJwt();
        }
    }
}
using System.Web.Mvc;
using TwilioChat.Web.Domain;

namespace TwilioChat.Web.Controllers
{
    public class TokenController : Controller
    {
        private readonly ITokenGenerator _tokenGenerator;

        public TokenController() : this(new TokenGenerator()) { }

        public TokenController(ITokenGenerator tokenGenerator)
        {
            _tokenGenerator = tokenGenerator;
        }

        // POST: Token
        [HttpPost]
        public ActionResult Index(string device, string identity)
        {
            if (device == null || identity == null) return null;

            const string appName = "TwilioChatDemo";
            var endpointId = string.Format("{0}:{1}:{2}", appName, identity, device);

            var token = _tokenGenerator.Generate(identity, endpointId);
            return Json(new {identity, token});
        }
    }
}
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-channel-image').on('click', showAddChannelInput);
        $('#leave-span').on('click', disconnectClient);
        $('#delete-channel-span').on('click', deleteCurrentChannel);
    });

    function handleUsernameInputKeypress(event) {
        if (event.keyCode === 13) {
            connectClientWithUsername();
        }
    }

    function handleInputTextKeypress(event) {
        if (event.keyCode === 13) {
            tc.currentChannel.sendMessage($(this).val());
            event.preventDefault();
            $(this).val('');
        }
        else {
            notifyTyping();
        }
    }

    var notifyTyping = $.throttle(function () {
        tc.currentChannel.typing();
    }, 1000);

    tc.handleNewChannelInputKeypress = function (event) {
        if (event.keyCode === 13) {
            tc.messagingClient.createChannel({
                friendlyName: $newChannelInput.val()
            }).then(hideAddChannelInput);
            $(this).val('');
            event.preventDefault();
        }
    };

    function connectClientWithUsername() {
        var usernameText = $usernameInput.val();
        $usernameInput.val('');
        if (usernameText == '') {
            alert('Username cannot be empty');
            return;
        }
        tc.username = usernameText;
        fetchAccessToken(tc.username, connectMessagingClient);
    }

    function fetchAccessToken(username, handler) {
        $.post('/token', {
            identity: username,
            device: 'browser'
        }, function (data) {
            handler(data);
        }, 'json');
    }

    function connectMessagingClient(tokenResponse) {
        // Initialize the IP messaging client
        tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
        tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
        updateConnectedUI();
        tc.loadChannelList(tc.joinGeneralChannel);
        tc.messagingClient.on('channelAdded', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('channelRemoved', $.throttle(tc.loadChannelList));
        tc.messagingClient.on('tokenExpired', refreshToken);
    }

    function refreshToken() {
        fetchAccessToken(tc.username, setNewToken);
    }

    function setNewToken(tokenResponse) {
        tc.accessManager.updateToken(tokenResponse.token);
    }

    function updateConnectedUI() {
        $('#username-span').text(tc.username);
        $statusRow.addClass('connected').removeClass('disconnected');
        tc.$messageList.addClass('connected').removeClass('disconnected');
        $connectPanel.addClass('connected').removeClass('disconnected');
        $inputText.addClass('with-shadow');
        $typingRow.addClass('connected').removeClass('disconnected');
    }

    tc.loadChannelList = function (handler) {
        if (tc.messagingClient === undefined) {
            console.log('Client is not initialized');
            return;
        }

        tc.messagingClient.getChannels().then(function (channels) {
            tc.channelArray = tc.sortChannelsByName(channels);
            $channelList.text('');
            tc.channelArray.forEach(addChannel);
            if (typeof handler === 'function') {
                handler();
            }
        });
    };

    tc.joinGeneralChannel = function () {
        console.log('Attempting to join "general" chat channel...');
        if (!tc.generalChannel) {
            // If it doesn't exist, let's create it
            tc.messagingClient.createChannel({
                uniqueName: GENERAL_CHANNEL_UNIQUE_NAME,
                friendlyName: GENERAL_CHANNEL_NAME
            }).then(function (channel) {
                console.log('Created general channel');
                tc.generalChannel = channel;
                tc.loadChannelList(tc.joinGeneralChannel);
            });
        }
        else {
            console.log('Found general channel:');
            setupChannel(tc.generalChannel);
        }
    };

    function setupChannel(channel) {
        // Join the channel
        channel.join().then(function (joinedChannel) {
            console.log('Joined channel ' + joinedChannel.friendlyName);
            leaveCurrentChannel();
            updateChannelUI(channel);
            tc.currentChannel = channel;
            tc.loadMessages();
            channel.on('messageAdded', tc.addMessageToList);
            channel.on('typingStarted', showTypingStarted);
            channel.on('typingEnded', hideTypingStarted);
            channel.on('memberJoined', notifyMemberJoined);
            channel.on('memberLeft', notifyMemberLeft);
            $inputText.prop('disabled', false).focus();
            tc.$messageList.text('');
        });
    }

    tc.loadMessages = function () {
        tc.currentChannel.getMessages(MESSAGES_HISTORY_LIMIT).then(function (messages) {
            messages.forEach(tc.addMessageToList);
        });
    };

    function leaveCurrentChannel() {
        if (tc.currentChannel) {
            tc.currentChannel.leave().then(function (leftChannel) {
                console.log('left ' + leftChannel.friendlyName);
                leftChannel.removeListener('messageAdded', tc.addMessageToList);
                leftChannel.removeListener('typingStarted', showTypingStarted);
                leftChannel.removeListener('typingEnded', hideTypingStarted);
                leftChannel.removeListener('memberJoined', notifyMemberJoined);
                leftChannel.removeListener('memberLeft', notifyMemberLeft);
            });
        }
    }

    tc.addMessageToList = function (message) {
        var rowDiv = $('<div>').addClass('row no-margin');
        rowDiv.loadTemplate($('#message-template'), {
            username: message.author,
            date: dateFormatter.getTodayDate(message.timestamp),
            body: message.body
        });
        if (message.author === tc.username) {
            rowDiv.addClass('own-message');
        }

        tc.$messageList.append(rowDiv);
        scrollToMessageListBottom();
    };

    function notifyMemberJoined(member) {
        notify(member.identity + ' joined the channel')
    }

    function notifyMemberLeft(member) {
        notify(member.identity + ' left the channel');
    }

    function notify(message) {
        var row = $('<div>').addClass('col-md-12');
        row.loadTemplate('#member-notification-template', {
            status: message
        });
        tc.$messageList.append(row);
        scrollToMessageListBottom();
    }

    function showTypingStarted(member) {
        $typingPlaceholder.text(member.identity + ' is typing...');
    }

    function hideTypingStarted(member) {
        $typingPlaceholder.text('');
    }

    function scrollToMessageListBottom() {
        tc.$messageList.scrollTop(tc.$messageList[0].scrollHeight);
    }

    function updateChannelUI(selectedChannel) {
        var channelElements = $('.channel-element').toArray();
        var channelElement = channelElements.filter(function (element) {
            return $(element).data().sid === selectedChannel.sid;
        });
        channelElement = $(channelElement);
        if (tc.currentChannelContainer === undefined && selectedChannel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.currentChannelContainer = channelElement;
        }
        tc.currentChannelContainer.removeClass('selected-channel').addClass('unselected-channel');
        channelElement.removeClass('unselected-channel').addClass('selected-channel');
        tc.currentChannelContainer = channelElement;
    }

    function showAddChannelInput() {
        if (tc.messagingClient) {
            $newChannelInputRow.addClass('showing').removeClass('not-showing');
            $channelList.addClass('showing').removeClass('not-showing');
            $newChannelInput.focus();
        }
    }

    function hideAddChannelInput() {
        $newChannelInputRow.addClass('not-showing').removeClass('showing');
        $channelList.addClass('not-showing').removeClass('showing');
        $newChannelInput.val('');
    }

    function addChannel(channel) {
        if (channel.uniqueName === GENERAL_CHANNEL_UNIQUE_NAME) {
            tc.generalChannel = channel;
        }
        var rowDiv = $('<div>').addClass('row channel-row');
        rowDiv.loadTemplate('#channel-template', {
            channelName: channel.friendlyName
        });

        var channelP = rowDiv.children().children().first();

        rowDiv.on('click', selectChannel);
        channelP.data('sid', channel.sid);
        if (tc.currentChannel && channel.sid === tc.currentChannel.sid) {
            tc.currentChannelContainer = channelP;
            channelP.addClass('selected-channel');
        }
        else {
            channelP.addClass('unselected-channel')
        }

        $channelList.append(rowDiv);
    }

    function deleteCurrentChannel() {
        if (!tc.currentChannel) {
            return;
        }
        if (tc.currentChannel.sid === tc.generalChannel.sid) {
            alert('You cannot delete the general channel');
            return;
        }
        tc.currentChannel.delete().then(function (channel) {
            console.log('channel: ' + channel.friendlyName + ' deleted');
            setupChannel(tc.generalChannel);
        });
    }

    function selectChannel(event) {
        var target = $(event.target);
        var channelSid = target.data().sid;
        var selectedChannel = tc.channelArray.filter(function (channel) {
            return channel.sid === channelSid;
        })[0];
        if (selectedChannel === tc.currentChannel) {
            return;
        }
        setupChannel(selectedChannel);
    };

    function disconnectClient() {
        leaveCurrentChannel();
        $channelList.text('');
        tc.$messageList.text('');
        channels = undefined;
        $statusRow.addClass('disconnected').removeClass('connected');
        tc.$messageList.addClass('disconnected').removeClass('connected');
        $connectPanel.addClass('disconnected').removeClass('connected');
        $inputText.removeClass('with-shadow');
        $typingRow.addClass('disconnected').removeClass('connected');
    }

    tc.sortChannelsByName = function (channels) {
        return channels.sort(function (a, b) {
            if (a.friendlyName === GENERAL_CHANNEL_NAME) {
                return -1;
            }
            if (b.friendlyName === GENERAL_CHANNEL_NAME) {
                return 1;
            }
            return a.friendlyName.localeCompare(b.friendlyName);
        });
    };

    return tc;
})();
var twiliochat = (function () {
    var tc = {};

    var GENERAL_CHANNEL_UNIQUE_NAME = 'general';
    var GENERAL_CHANNEL_NAME = 'General Channel';
    var MESSAGES_HISTORY_LIMIT = 50;

    var $channelList;
    var $inputText;
    var $usernameInput;
    var $statusRow;
    var $connectPanel;
    var $newChannelInputRow;
    var $newChannelInput;
    var $typingRow;
    var $typingPlaceholder;

    $(document).ready(function () {
        tc.$messageList = $('#message-list');
        $channelList = $('#channel-list');
        $inputText = $('#input-text');
        $usernameInput = $('#username-input');
        $statusRow = $('#status-row');
        $connectPanel = $('#connect-panel');
        $newChannelInputRow = $('#new-channel-input-row');
        $newChannelInput = $('#new-channel-input');
        $typingRow = $('#typing-row');
        $typingPlaceholder = $('#typing-placeholder');
        $usernameInput.focus();
        $usernameInput.on('keypress', handleUsernameInputKeypress);
        $inputText.on('keypress', handleInputTextKeypress);
        $newChannelInput.on('keypress', tc.handleNewChannelInputKeypress);
        $('#connect-image').on('click', connectClientWithUsername);
        $('#add-chan