$(document).ready(function () {
  var socket = io.connect();
  var messageTemplate = _.template($('#message-template').html());

  socket.on('message recieved', function (data) {
    $('#message-list').append(messageTemplate(data));
    $('#message-list').scrollTop($('#message-list').height());
  });

  function sendMessage() {
    socket.emit('message sent', {
      name: $('#name-field').val(),
      message: $('#message-field').val()
    });
    $('#message-field').val('');
  }

  $('#message-send-button').click(sendMessage);

  $('#message-field').on('keypress', function (event) {
    var returnKey = 13;

    if (event.which === returnKey) {
      sendMessage();
    }
  });
});

$(document).ready(function () {
  var socket = io.connect();
  var messageTemplate = _.template($('#message-template').html());

  // Elements
  var $messageList = $('#message-list');
  var $nameField = $('#name-field');
  var $messageField = $('#message-field');
  var $messageSendButton = $('#message-send-button');

  socket.on('message recieved', function (data) {
    $messageList.append(messageTemplate(data));
    $messageList.scrollTop($messageList.height());
  });

  function sendMessage() {
    socket.emit('message sent', {
      name: $nameField.val(),
      message: $messageField.val()
    });
    $messageField.val('');
  }

  $messageSendButton.click(sendMessage);

  $messageField.on('keypress', function (event) {
    var returnKey = 13;

    if (event.which === returnKey) {
      sendMessage();
    }
  });
});

Save frequently used jQuery elements to variables for caching purposes
Save frequently used jQuery elements to variables for caching purposes
