简体   繁体   中英

socket.io on node.js and heroku, idle timeout error

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, ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM