I'm trying to send data from a client to an endpoint in nodejs and with socket io graph this in real time on a temperatura vs tiempo
.
Client side:
else{
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);
sleep(1000);
};
The data is sent normally to the indicated endpoint.
This is the part of the server where the endpoint that receives the request is
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(err);
res.send(err);
}
else{
io.sockets.on("connection",function(socket){
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
socket.emit('post', date.getTime() - ( date.getTimezoneOffset() * 60000) , temp );
})
console.log("Dato creado exitosamente");
}
});
});
return router;
}
This is the part of the view that receives the data that comes by post (it should be that way but they are not shown, otherwise I have to update for the changes to be effective).
var socket = io.connect('http://localhost:7777/');
var chart;
chart = new Highcharts.Chart({
chart: {
renderTo: 'chart',
defaultSeriesType: 'spline',
events: {
load: function() {
// Cada vez que reciba un valor desde el socket, lo meto en la gráfica
socket.on('post', function ( time, data ) {
console.log("LLEGO DATO O NO?");
console.log(data);
console.log("TIEMPO");
console.log(time);
var series = chart.series[0];
series.addPoint([time, data]);
});
}
}
},
rangeSelector : {
selected : 100
},
title: {
text: 'Datos del Sensor de temperatura'
},
xAxis: {
type:'datetime',
tickPixelInterval: 50,
maxZoom: 20 * 1000
},
yAxis: {
minPadding: 0.1,
maxPadding: 0.1,
title: {
text: 'Valores',
margin: 10
}
},
series: [{
name: 'Sensor Temperatura',
data: []
}]
});
The issue is in the part of the endpoint that receives the data, they arrive intact, but when I check I see that the socket is skipped and it sends me the message console.log("Data created successfully"); , and the states in the graph in highcharts are not shown in real time, for the changes to be effective I have to update the window, and only then do the data appear in the console (the ones that should appear before sending the data).
edit:
Seeing the logs with morgan, when the data is sent, the 2 take the same date / time, I suppose this is due to the synchronization issue that I must put in date , and also the post request made is not shown until the client is closed , it's like keeping the connection alive.
Well, the solutions were just putting it
io.sockets.emit
on a line and deletingio.sockets.on("connection"...
it because I'm not going to listen to events, just send them.