Won't that HTTP header also cause the connection to remain open for a long time? So what is the advantage?
Can anyone please clarify for me? I seem to have missed the concept, I think.
Won't that HTTP header also cause the connection to remain open for a long time? So what is the advantage?
Can anyone please clarify for me? I seem to have missed the concept, I think.
At the TCP/IP level it looks the same: a socket is open.
But from the browser point of view they are completely different. The keep-alive is for the browser to re-use to request more content (e.g. images, css files, next page on the site). WebSockets is for two-way communication from within your Javascript application code. The server can choose to send content at any time. Your JS application can send data to the server at any time.
Also worth comparing to SSE (aka EventSource), which also allows the server to choose to send content at any time, but is one-way (your JS application has to resort to using XHR when it needs to send more data). (A full comparison of WebSockets and SSE can get very complex, so I'll say no more here, except to say that SSE can often be the correct choice.)
Also compare to Server Push in HTTP/2 (aka SPDY). This is for the server to proactively push files (images, css files, next page on the site), but it is at the browser-level again, not controlled from Javascript.