1

Socket.IO uses heartbeat pattern to be sure that a client is still connected. It would solve my algorithm if I would be able to subscribe to the hearbeat event on server.

for instance: socket.on "heartbeat", -> mycode

any idea if this is possible?

jcollum
  • 43,623
  • 55
  • 191
  • 321
Luman75
  • 878
  • 1
  • 11
  • 28

1 Answers1

1

The heartbeat happens very frequently, are you sure that's the right thing? I use the "connect" event, that seems like it might do what you need:

  connect: (url, options, connectCallback) =>
    someFunction = =>
      @doSomeRedisStuff() if @connected

    @logger.debug "starting connection to url: #{url}"
    @socket = @socket.connect(url, options)
    @logger.debug "connecting ..."
    @socket.on "connect", =>    
      @logger.info "connected (socket id #{(@socket.socket.sessionid)})"
      @connected = true 
      setTimeout someFunction, 2000
      connectCallback() if connectCallback?
     @socket.on "disconnect", =>
       @connected = false 

If the connection to the server is interrupted (e.g. the server is restarted) the connect event will kick off when the connection comes back. Socket.IO is smart about this.

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • connect if one time per session or per part of the session. When the client has a good network then the connection might last for hours or in my application case days. I would like to know that the connection is alive in the meantime. – Luman75 Aug 19 '13 at 17:03
  • @Luman75 there's a `socket.on 'disconnect', -> ` as well. I'd just store the connection state somewhere and update in those two callbacks. – jcollum Aug 19 '13 at 17:33
  • of course there is disconnect. But I would like to "poke" some redis variables during the ongoing socket.io connection. – Luman75 Aug 19 '13 at 17:39
  • @Luman75 I think you should set and clear a repeating action with setTimeout in the disconnect/connect callbacks: http://stackoverflow.com/questions/11440132/in-socket-io-is-heartbeat-an-event-that-can-be-used-to-trigger-other-actions – jcollum Aug 19 '13 at 18:05
  • Ok. if you call someFunction recursively with some sleep. Then yes, that whould work. Unless memory leakage might be an issue here. – Luman75 Aug 19 '13 at 18:16
  • @Luman75 it's got a 2000 sleep on it, that's where you'd adjust the freq. – jcollum Aug 19 '13 at 23:13