The problem of out-of-order messages

  node.js, question

After receiving the client message, the server calls the function to call back and broadcast the message. Due to asynchronous callback, the order of server-side broadcasting messages is inconsistent with the order of receiving messages. How to ensure consistency?
The code is as follows:
socket.on(‘message’, function (data) {

function(callback(err, res){
 socket.emit('message', res);


The server accepts messagesa-b; The sending order may be b-a.
How to ensure that the sending order is also A-B?

This is very common in instant messaging inside, which is much more complicated than this. It is better to add serial number or version number control.
It is not a small project and cannot be solved by changing one or two lines of code.