After creating the order, the client goes to the page where the bots respond to him at random intervals (to fulfill his order). Bots push in the socket via php, nodejs accepts and renders on the page.
Bots must periodically push messages to the chat, do it asynchronously, because bots from the server can come in a random time interval.
Code on nodejs
managementNsp.on('connection', function(socket){ socket.on('addBid', function (data) { if (data.writer.isBot) { function asyncPushToChat() { var messagesArray = ["Hello", "Test message"]; messagesArray.forEach(function(item) { setTimeout(function(){ push(item); }, randomSeconds); }); } } }); How to make asyncPushToChat function asynchronous ??
if so rewritten for promises, is that correct?
managementNsp.on('connection', function(socket){ socket.on('addBid', function (data) { if (data.writer.isBot) { function asyncPushMessageToChat() { return new Promise(function(resolve, reject) { connection.query("SELECT text FROM bot_message", function (err, messagesArray) { if (err) { reject(err); } messagesArray.forEach(function(item) { setTimeout(function(){ return resolve(item); }, randomSeconds); }); }); }); } asyncPushMessageToChat() .then(function(res) { //append to html }) .catch(function(err){ console.log(err); }); } });