Socket I.o does not update automatically

1

Hello, I'm using the Socket i.o and when I send a POST / EnviaTemperatura, it is not automatically updating on my site. What am I doing wrong?

Node.js

app.post('/EnviaTemperatura', function(req, res){
    temperatura = req.body.temp;
    console.log(temperatura);
    res.send('Temperatura: ' + temperatura);
});

io.on("connection", function(socket) {
    socket.emit('RecebTemp', temperatura);
});

HTML

<div class="col-xs-9 text-right">
    <div class="huge"><span id="EnviaTemp">0</span> ºC</div>
    <div>Temperatura no Interior da casa</div>
</div>

<script>
    var socket = io();
        socket.on('RecebTemp', function (temperatura) { 
            document.getElementById("EnviaTemp").innerHTML = temperatura;
        });
</script>

In my case, it is only updating when I do a Refresh on the page. But I did not want to refresh the entire page, only in this DIV, since I have other elements on the screen that take a while to load.

What could it be?

    
asked by anonymous 05.10.2016 / 14:13

1 answer

1

In your node.js code, change to:

app.post('/EnviaTemperatura', function(req, res){
    temperatura = req.body.temp;
    io.sockets.emit("RecebTemp", temperatura);
    console.log(temperatura);
    res.send('Temperatura: ' + temperatura);
});

And remove this entire snippet:

io.on("connection", function(socket) {
    socket.emit('RecebTemp', temperatura);
});
    
05.10.2016 / 18:35