node.js - Observable calls function multiple times with single change -
i writing node.js application reading value , doing calculations on interval. if value changes modifies observable calls function emit sendval. application works great 1 user, problem when multiple users connect , value changes each user receives same notification multiple times (the same number of notifications concurrent users).
i think using observable incorrectly, can't figure out why i'm getting multiple messages single change.
server code:
var app = require('express')(); var http = require('http').server(app); var io = require('socket.io')(http); var o = require('observable'); var reportedtotal = o(0); app.all('/', function (req, res, next) { res.header("access-control-allow-origin", "*"); res.header("access-control-allow-headers", "x-requested-with"); next(); }); io.on('connection', function (socket) { console.log('connected'); reportedtotal(function(val) { io.emit('sendval', json.stringify({ lvl: val })); }); socket.on('disconnect', function() { console.log('disconnected'); }); }); http.listen(3000, function () { console.log('listening on *:3000'); }); setinterval(function () { var tot = 0; //does stuff here if(changed) { reportedtotal(tot); } }, 1000); client code:
var url = <server ip>; var socket = io(url); socket.on('sendval', function(msg){ $('#messages').append($('<li>').text(msg)); });
Comments
Post a Comment