Greetings to all!

There was a task, in the interval every 5 minutes to send, to everyone who is on the page a certain action, how to send?

var app = require('http').createServer(handler), io = require('socket.io').listen(app), static = require('node-static'); // for serving files var fileServer = new static.Server('./'); // Users var clients = 0; app.listen(8080); function handler (request, response) { request.addListener('end', function () { fileServer.serve(request, response); }); } setInterval(function(){ io.sockets.emit('timer_sec', {'text':'Таймер прошел!'}) }, 10000); io.set('log level', 1); io.sockets.on('connection', function (socket) { ++clients; socket.on('mousemove', function (data) { socket.broadcast.emit('moving', data); }); socket.on('disconnect', function () { —clients; }); }); 
  • one
    Perhaps I did not correctly understand the question, but - what is the difficulty? - Zowie
  • The server part starts when a person connects to socket.io.connect, i.e. for all 5 minutes will be at different times. A need is not important. a person is 2 minutes per page .. and if the timer has already been 3 minutes before him, then he will have to give it. - V_Tjuryakin
  • If for all 5 minutes different, then for each client you need your own timer. I think you yourself understand this, but you thought that there was some great magic that you didn’t know about :) And if you are afraid, timers themselves will not lead to performance problems. Nevertheless - for a more accurate answer, please describe in more detail what exactly and how you are going to do - Zowie
  • No, 5 minutes should go in the interval, it does not matter if a person came in 4 minutes ago, 5 minutes ago or 30 seconds ago - if the timer has passed, do it. - V_Tjuryakin
  • one
    For the future - do not aggro on those who are trying to help you, for example, I do this for the first and last time. I, of course, could post a test under phantom.js, post it, post a conclusion, but not this time - Zowie

1 answer 1

Well then setInterval(function(){ io.sockets.emit('e', {...}) },5*60*1000) , isn't it obvious

  • It turns out to use this function earlier than io.sockets.on ('connection', function (socket) {}); - I was interested in it! - V_Tjuryakin
  • one
    @V_Tjuryakin, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). By the way, since you didn’t know this, then you definitely wouldn’t help to read the docks, wikis, and see several source codes. - Zowie
  • I didn’t click for one reason that I asked a question, but you thought I was arguing and did not disprove, so I answered myself the question myself;) - V_Tjuryakin
  • So you have not quite correctly formulated the question, you can only blame yourself - Zowie
  • one
    And how did you determine that "it re-considers"? I have suspicions that you simply did not take into account the time to connect and you just had the impression, try in several browsers and you will see that everything works. PS: if you are asked to lay out the code - just do it, then that the sockets work, I already understood. PPS: from nefig do wrote a test, looked, everything works fine, crooked testing and incorrect conclusions is not my problem - Zowie