Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how is websocket different than http with header connection-keep-alive=million

Tags:

websocket

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.

like image 396
Muhammad Umer Avatar asked Jul 12 '13 06:07

Muhammad Umer


People also ask

How is WebSocket different from HTTP?

Unlike HTTP, where you have to constantly request updates, with websockets, updates are sent immediately when they are available. WebSockets keeps a single, persistent connection open while eliminating latency problems that arise with HTTP request/response-based methods.

Does WebSocket keep connection alive?

WebSocket uses HTTP as the initial transport mechanism, but keeps the TCP connection alive after the HTTP response is received so that it can be used for sending messages between client and server.

Are WebSockets more efficient than HTTP?

All the frequently updated applications used WebSocket because it is faster than HTTP Connection. When we do not want to retain a connection for a particular amount of time or reuse the connection for transmitting data; An HTTP connection is slower than WebSockets.

How much faster is WebSocket?

Plain WebSocket can be up to 3.7 times as fast to receive a message from the server compared to Socket.IO and 1.7 times as fast compared to SockJS. Plain WebSocket scales well in terms of response time and memory requirement with higher concurrency levels.


1 Answers

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.

like image 94
Darren Cook Avatar answered Oct 01 '22 18:10

Darren Cook