title: Refactoring from ajax to websockets: recommended patterns and quick wins
body: <p>I have a single page application that currently uses ajax and REST for talking to the server.  I structure the code mostly using promises and deferreds, and I use a pubsub broker to communicate between components.  In general, the code is structured to follow <a href="http://www.youtube.com/watch?v=vXjVFPosQHw" rel="nofollow noreferrer">Zakas' suggestions for scalable application architecture</a>.</p>
  
  <p>I'd like to move at least some of the interaction with the server into websockets both for performance and ease of development.  Some specific interactions I plan to change to use websockets are:</p>
  
  <ul>
  <li>Chat functionality</li>
  <li>Feedback on long running tasks</li>
  <li>Updates to attributes of currently viewed objects (e.g. someone POSTs to a REST endpoint that corresponds to an object I'm currently rendering)</li>
  </ul>
  
  <p>My question is:</p>
  
  <ul>
  <li>What are some other quick wins (i.e. websockets >> ajax for a given use case) I should look out for?</li>
  <li>What <a href="http://addyosmani.com/resources/essentialjsdesignpatterns/book/" rel="nofollow noreferrer">design patterns</a> are most helpful in structuring websocket code?</li>
  <li>Should some interactions that should stay RESTful ajax calls, or is there a strong case for going 100% websockets?</li>
  </ul>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Hera are some related questions I found after I submitted this.  These deal most closely with the last part of my question (does going 100% websockets make sense).</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4848642/what-is-the-disadvantage-of-using-websocket-socket-io-where-ajax-will-do">What is the disadvantage of using websocket/socket.io where ajax will do?</a></li>
  <li><a href="https://stackoverflow.com/questions/6806263/websocket-api-to-replace-rest-api">websocket api to replace rest api?</a></li>
  <li><a href="https://stackoverflow.com/questions/10377384/why-use-ajax-when-websockets-is-available">Why use AJAX when WebSockets is available?</a></li>
  <li><a href="https://stackoverflow.com/questions/4666571/with-websockets-is-there-a-place-for-ajax?rq=1">With websockets, is there a place for AJAX?</a></li>
  </ul>