Hi,

I have a Javascript script that is listening for incoming messages (via MQTT). After a message is received, I can execute a callback message. So far, on the callback, I have this:

function parse(message) {
    $actions.SaveDeviceDataInDatabase(message.destinationName + ': ' + message.payloadString);
}

I am calling a client action that will be writing the message in the database.

The problem is that I can receive a lot of message in a short interval of time. As of my knowledge, client actions are not asynchronous, so, if I am currently inside the client action writing a message in the DB and another message arrives, the callback will not be able to call the client action and the message will be lost. Is this what happens?

I will be running some tests to check if this solution works, but I would like your opinion if this is the best approach or if there is a better one. I also thought of writing the incoming messages in a container on the callback and then call the client action which will retrieve the messages from the container, but that solution doesn't seem reliable as well. I am out of ideas.


Thank you,

João Lopes

Hi João,

JavaScript is single-threaded, this means, that the thread executing the code will changes context of execution, when an event occurs, but gets back to the previous code as soon as it's done. 

As to my knowledge, the only thing that you cannot guarantee is that the order in which the messages are stored to the database is the exact order in which those messages arrived.

The key thing here, is that there is no multi-threading (without service workers of course) in JavaScript!

This being said, please share the results of your tests. :)

Cheers,

RG

Ruben Goncalves wrote:

Hi João,

JavaScript is single-threaded, this means, that the thread executing the code will changes context of execution, when an event occurs, but gets back to the previous code as soon as it's done. 

As to my knowledge, the only thing that you cannot guarantee is that the order in which the messages are stored to the database is the exact order in which those messages arrived.

The key thing here, is that there is no multi-threading (without service workers of course) in JavaScript!

This being said, please share the results of your tests. :)

Cheers,

RG

If that is indeed the case, I have the message timestamp, so that won't be a problem. I will share my results once I have them. 


Thank you for your reply,

João Lopes