2

I'm using socket.io, which works great locally, but once in production with heroku it times out. I know this is because heroku will kill/timout a web socket connection if nothing is heard inside the 55 second rolling window.

The problem I've got is that I'm setting the socket.io hearbeat interval to 60000 so nothing is heard for 60 seconds, i.e outside the 55 second window:

io.set('heartbeat interval', 60000); //heroku timeout with Idle connection error. 

I need some way of polling the server inside that 55 second window that keeps the connection alive. Changing the heartbeat interval isn't really an option, so I'm not sure how to go about this.

OliverJ90
  • 1,291
  • 2
  • 21
  • 42
  • will this work for you? http://stackoverflow.com/questions/17133134/how-to-keep-alive-a-socket-in-node-js-in-appfog – ilj Sep 28 '15 at 15:11
  • That probably won't do it. I'm handling the connect/disconect events gracefully but with user notifications etc. It will not work like that as there will be a whole process of disconnect and connect events firing every 60 seconds. – OliverJ90 Sep 28 '15 at 15:13
  • why can't you set it to be smaller than heroku timeout? – ilj Sep 28 '15 at 15:17
  • my application employs a 'bill by the minute' logic for client connection so measures the connection times via the heartbeat. I could effectively do a "bill by the 54 seconds' but that's not ideal. – OliverJ90 Sep 28 '15 at 15:19
  • maybe queue client requests on client side then check for socket status, reopen it if required, then send? – ilj Sep 28 '15 at 15:28
  • I just changed it to a 30 second interval. It's not a huge deal just means the billing will be in 1/2 min increments. – OliverJ90 Sep 28 '15 at 15:37
  • or 30 seconds and then calculate the time accordingly. but queuing seems more elegant and future-proof to me. when will you do if they change it to 29 secs? :-) – ilj Sep 28 '15 at 15:39

1 Answers1

0

In general, you want your router to disconnect idle connections so they don't consume system resources on the backend. The challenge is separating actually idle / lost connections and realtime-connected clients that just haven't said anything for a while.

One solution is just having the clients ping the server within the 55-second window. An example of that is here:

https://github.com/hunterloftis/websocket-ping/blob/2dd45b100a754ee8b151ab3d16d2ee3787d2dc8e/index.html#L65

https://github.com/hunterloftis/websocket-ping/blob/2dd45b100a754ee8b151ab3d16d2ee3787d2dc8e/index.html#L80-L83

Basically just io.emit('ping') on a setInterval. That way, as long as the client is alive, it will continue to maintain its connection with the server.

hunterloftis
  • 13,386
  • 5
  • 48
  • 50