reactphp / socket

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP.
https://reactphp.org/socket/
MIT License
1.2k stars 156 forks source link

Keep tcp connection alive #249

Closed albosmart-ro closed 4 years ago

albosmart-ro commented 4 years ago

How do i keep the tcp connection alive?

WyriHaximus commented 4 years ago

You don't have to, TCP connections stay connected as long as nothing between both peers puts a timeout on it. You can send a heartbeat over it every X seconds to ensure it is still alive. But aside from that it should just stay connected

clue commented 4 years ago

What @WyriHaximus said :-)

TCP/IP connections will be kept alive until one side decides to close the connection. It's perfectly fine to not transmit anything over an idle connection for hours or even days (YMMV).

Note that depending on your network setup, you may have a number of middleboxes between both connection endpoints which may limit the lifetime of a connection. Many load balancers or NAT devices are known to terminate idle connections after a while, a timeout of 60 or 600 seconds is quite common.

If you're experiencing timeouts, you may want to occasionally transmit some data over the connection to keep it alive. Many protocols provide some mechanisms (framing) to support keep-alive or heartbeat messages.

In many cases, closing idle connections can be considered a good thing as it allows freeing up some resources.

I hope this helps :+1: