title: Should I be using AJAX or WebSockets.
body: <p>Oh the joyous question of <code>HTTP</code> vs <code>WebSockets</code> is at it again, however even after quit a bit of reading on the hundreds of <code>versus</code> blog posts, SO questions, etc, etc.. I'm still at a complete loss as to what I should be working towards for our application. In this post I will be supplying information on application functionality, and the types of requests/responses used in our application currently. </p>
  
  <p>Currently our application is a sloppy piece of work, thrown together using <code>AngularJS</code> and <code>AJAX</code> requests to a <code>Apache</code> server running <code>PHP</code>, namely <code>XAMPP</code>. With the launch of our application I've noticed that we're having problems with response times when the server is under any kind of load. This probably has something to do with the sloppy architecture of our server, the hardware, and the fact that our MySQL database isn't exactly optimized. </p>
  
  <p>However, with such a loyal fanbase and investors seeing potential in our application and giving us a chance to roll out a <code>2.0</code> I've been studying hard into how to turn this application into a powerhouse of low latency scalability. Honestly the best option would be <code>hire someone with experience</code>, but unfortunately I'm a hobbyist, and a one-man-army without much experience. </p>
  
  <p>After some extensive research, I've decided on writing the backend using <code>NodeJS</code> this time. However I'm having a hard time deciding on <code>HTTP</code> or <code>Websockets</code>. Here's the types of transactions that are done between the Server/Client.</p>
  
  <ul>
  <li><p>Client sends a request to the server in JSON format. The request has a few different things. </p>
  
  <ul>
  <li>A request id (For processing logic based on the request)</li>
  <li>The data associated with the request ID.</li>
  </ul></li>
  <li><p>The server receives the request, polls the database (if necessary) and then responds to the client in JSON format. Sometimes the server is serving files to the client. Namely images in Base64 format.</p></li>
  </ul>
  
  <p>Currently the application (When being used) sends a request to the server every time an interface is changed, which on average for our application is once every few seconds. Every action on our interfaces sends another request to the server. The application also sends requests to check for notifications/messages every 8 seconds, (or two seconds depending on if they're on the messaging interface). </p>
  
  <p>Currently here are the benefits I see of a <code>stated</code> connection over a <code>stateless</code> connection with our application.</p>
  
  <ul>
  <li><p>If the connection is stated, I can eliminate the requests for notifications and messages, as the server can just tell the client whenever one comes available. This can eliminate <code>x(n)/4</code> requests per second to the server alone. </p></li>
  <li><p>Handling something like a disconnection from the server is as simple as attempting to reconnect, opposed to handling timeouts/errors per request, this would only be handled on the socket.</p></li>
  <li><p>Additional security can be obtained by removing security keys for database interaction, this should prevent the possibility of <code>Hijacking(?)</code> of a session_key and using it to manipulate or access another users data. The session_key is only needed due to there being no state in the <code>AJAX</code> setup.</p></li>
  </ul>
  
  <p>However, I'm someone who started learning programming through TCP game server emulation. So I understand <code>some</code> benefits of a STATED connection, while I don't understand the benefits of a STATELESS connection very much at all. I know they both have their benefits and quirks, but I'm curious what would be the best approach for us.</p>
  
  <p>We're mainly looking for Scalability, as we had a local application launch and managed to bottleneck at nearly 10,000 users in under 48 hours. Luckily I announced this as a <code>BETA</code> and the users are cutting me a lot of slack after learning that I did it all on my own as a learning project. I've disabled registrations while looking into improving the application's front and backend. </p>
  
  <p><strong>IMPORTANT:</strong></p>
  
  <p>If using WebSockets, would we be able to <code>asynchronously</code> download pictures from the server like we can with <code>AJAX</code>? For example, I can make 5 requests to the server using AJAX for 5 different images, and they will all start downloading immediately, using a stated connection would I have to wait for each photo to be streamed before moving to the next request? Would this only bottle-neck a single user, or every user that is waiting on a request to be completed? </p>