title: Can WebSockets replace AJAX when it comes to Database requests?
body: <p>This may seem like an extremely dumb question, but I am currently switching my Website from using the EventSource Polling constructor to the WebSocket standard which is implemented in Node.js. </p>
  
  <p>Originally, all backend on my website was handled with PHP. With the introduction of Node.js, I am trying to switch as much as I can without going outside of the "standard". By standard, I meaning that typically I see WebSocket implementations that send small data, and receive small data back vs. performing database queries and then sending large amounts of data back to the client.</p>
  
  <p><strong>Can WebSockets replace AJAX when it comes to Database requests?</strong></p>
  
  <hr>
  
  <p>Let's consider a small hello world program in <code>PHP/JavaScript (AJAX)</code> vs <code>Node.js/JavaScript (WebSockets)</code></p>
  
  <h2>    PHP/JavaScript (AJAX)</h2>
  
  <pre><code>// HelloWorld.php with Laravel in the Backend
  
  Table::update([ 'column' =&gt; $_POST['message'] ]);
  echo $_POST['message'];
  </code></pre>
  
  <p>Ajax.js with a custom ajax function</p>
  
  <pre><code>Global.request("HelloWorld.php").post({
       message: "Hello World"
  }).then(message =&gt; alert(message));
  </code></pre>
  
  <h2>    Node.js/JavaScript (WebSockets)</h2>
  
  <pre><code>// skip all the server setup
  server.on('connection', function () {
       server.on('message', function (message) {
           sqlConnection.query("UPDATE `table` SET `column` = ?", [message], function () {
                server.send(message);
           });
       });
  });
  </code></pre>
  
  <p>WebSocket.js:</p>
  
  <pre><code>let socket = new WebSocket('ws://example.com');
  
  socket.onmessage = function (message) {
      alert(message)
  }
  socket.send("Hello World");
  </code></pre>
  
  <p>They both essentially do the same thing, in a slightly different way. Now, in this scale it would not make sense to use WebSockets. Though an example, imagine it scaled up to a point where Node.js is processing bigger queries and sending lots of data to the client. Is this acceptable?</p>