Sunday 15 August 2010

node.js - SocketIO broadcasting too fast -


I would like to broadcast a message in every message to every customer (think of custom heartbeat system).

So the nods apps have started, the chairs are made and heartbeat messages are broadcast when I connect to the client app. I am still developing client applications and this means that every time we hit F5 and reload the application. When a new customer is loaded, SocketIo connection is made and it results in heartbeat messages coming in the client app with a rate higher than 1 message / sec.

There is nothing special about code-server side:

  var server = http.createServer (app); Var io = Required ('socket.io'). Listen (server); Server.listen (8080); Io.sockets.on ('connection', function (socket) {... setInterval (function () {console.info ('broadcast heartbeat'); socket.broadcast.emit ('heartbeat', / * Custom heart beat * /);}, 1000); ...});   

Client side:

  var socket = io.connect ('localhost', {'reconnect': false, port: 8080}); Socket.on ('heartbeat', function (data) {console.log ('heart beat');});   

Can someone give me some advice on what is wrong? Thank you

There is no need to start an interval every time you can archive the interval, and It can also clear with clear interval (INTERVAL); This is not required.

  var server = http.createServer (app); Var io = Required ('socket.io'). Listen (server); Server.listen (8080); Var INTERVAL; Io.sockets.on ('connection', function (socket) {... if (! INTERVAL) {INTERVAL = setInterval (function () {console.info ('broadcast heartbeat'); socket.broadcast.emit 'Heartbeat', / * custom heart beat * /);}, 1000);} ...});    

No comments:

Post a Comment