I have n names that simulate clients what I want to do is iterate over each of them and send me requests to an endpoint in nodejs
Here I have the part of the code corresponding to what I say to them
sensors=["Rimac","Cercado"];
for (sensor in sensors){
var date = new Date().toJSON();
var datos={
name:sensors[sensor],
valor:Math.random()*20,
fecha:date
};
console.log(datos);
var options = {
uri: 'http://localhost:7777/api/sensor',
method: 'POST',
headers: {
"content-type": "application/json",
},
json: datos
};
request(options,function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
});
sleep(1000);
};
The data arrives at the enpoint normally but, if I put a for to the previous part so it looks like this
for(i=0;i<100;i++){
for (sensor in sensors){
var date = new Date().toJSON();
var datos={
name:sensors[sensor],
valor:Math.random()*20,
fecha:date
};
console.log(datos);
var options = {
uri: 'http://localhost:7777/api/sensor',
method: 'POST',
headers: {
"content-type": "application/json",
},
json: datos
};
request(options,function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
});
sleep(10000);
};
}
The data does not reach the endpoint, why is it? It seems to me that it is due to an asynchronous issue.
Edito1
Good I add them the cod that controls the subject of the post in the servant
module.exports=function(app,Sensor,io){
var express=require("express");
var router=require("express").Router();
router.route("/sensor").get(function (req, res){
Sensor.find(
function(err, Sensor) {
if (err)
res.send(err)
else{
console.log("Exito al retornar todo");
res.json(Sensor);
}
// devuelve todas las Sensors en JSON
}
);
}).post(function(req,res){
console.log("BODY=");
console.log(req.body)
Sensor.create(
{
name:req.body.name,
Valor:req.body.valor,
Fecha:req.body.fecha
} , function(err) {
if (err){
console.log("HUBO UN ERROR",err);
res.send(err);
}
else{
var temp = parseFloat(req.body.valor);
var date = new Date(req.body.fecha);
console.log(date);
console.log(temp);
//Se pasan los datos a el cliente web ahi se manejara para mostrar la grafica en tiempo real mediante higcharts
io.sockets.emit('post', date.getTime() - ( date.getTimezoneOffset() * 60000) , temp ,req.body.name);
console.log("Dato creado exitosamente");
}
});
});
return router;
}
Image of the client when sending the data, WITHOUT USING THE FOR UP TO 100, that is, only one instance is sent
Image of the server when receiving this instance, until here everything goes well what I want, the problem is when I want to emulate 100 200 or n requests.
AQui when I add the for to 100, to have 100 requests from both Rimac and Cercado.
Sending data link
Server does NOT receive data
There is also the issue of for example how is a for everything sent in a linear way, how would you add synchronism to the requests? , I thought about threads, I come from java, if you have examples please share it.
Edito3
As I did for 100 requests in 100 seconds I was not able to reach the end reduced to 10 and I see that the requests are sent, but they are sent in block to what I mean, that the client for there the server comes out, what I want is that they are sent constantly not in block.
Also, the request callback on the client is not executed, but when I close the server.