Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long can a websocket connection last?

Tags:

websocket

How long can a client be connected to a server via websockets? Is there a time limit, can they potentially be connected together for years?

like image 648
Shai UI Avatar asked May 06 '18 07:05

Shai UI


People also ask

Do WebSockets time out?

A WebSocket times out if no read or write activity occurs and no Ping messages are received within the configured timeout period. The container enforces a 30-second timeout period as the default. If the timeout period is set to -1 , no timeout period is set for the connection.

Are WebSocket connections persistent?

A WebSocket is a persistent connection between a client and server. WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection. At its core, the WebSocket protocol facilitates message passing between a client and server.

Does WebSocket keep connection open?

WebSockets keeps a single, persistent connection open while eliminating latency problems that arise with HTTP request/response-based methods.


1 Answers

A WebSocket connection can in theory last forever. Assuming the endpoints remain up, one common reason why long-lived TCP connections eventually terminate is inactivity. WebSockets have a ping-pong mechanism which among other things can avoid closure by "smart" network routers which often have an inactivity timeout of a few hours. But if your application actively sends data (in either direction) at least once an hour, that's probably enough.

Still, "forever" is unlikely to be achieved in practice, because TCP connections on most networks eventually get terminated by some outage or other. You'll want intelligent reconnection logic if reliability is important.

like image 166
John Zwinck Avatar answered Sep 21 '22 18:09

John Zwinck