nodejs websocket store data async

Viewed 53

Im trying to save data to a MySQL database, that has been received from a websocket connection. Right now, this is how i do it:

WebSocket.permaConnection(
    received(data) {
      pool.getConnection(function (err, connection) {
          if (err) {
              return console.log(err.message);
          };
          var sql = `INSERT data)`
          connection.query(sql, function (err, result) {
              if (err) throw err;
          });
          connection.release();
      }
   }
)

But I wonder if thats the right way to do it, because the websocket could possibly send data, when the insert is not yet finished. That seems not right. Would an async insert-function solve this problem? And how would you fix it?

Any hint in the right direction would be highly appreciated! Thank you and happy easter!

0 Answers
Related