I have a page with a button. When a button is pressed, it receives data through socket.io. However, every time I reload the page, socket.io will send back one extra copy of the data from the previous data set. Thus, my data will look good when the page loads (example: abcd). Then reload the page, return 2n data (abcdabcd), reload the page again. I get 3n data (abcdabcdabcd), etc.
How to avoid duplication of data sent back to the client when the page reloads? Here is my code.
Server side:
app.get('/test', function(req, res){
io.sockets.on("connection", function(socket){
var socketFn = function(data){
socket.emit("trends", {
trends: JSON.parse(redisData)
});
};
socket.on("action", socketFn);
socket.on("disconnect", function(){
socket.removeListener("action", socketFn);
});
});
res.render('test');
});
Client side:
var socketOpts = {
"sync disconnect on unload" : true
};
var socket = io.connect("", socketOpts);
socket.on("trends", function(data){
});
function action(){
socket.emit("action", {phrase: "some dummy data"});
return false;
}
$("button#click").off("click").on("click", action);
source
share