Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use websocket and what is the advantage of using it? [closed]

Tags:

I tried reading some articles, but not so clear on this topic.

Would someone like to explain me below points:

  • Why use websocket over http
  • what is full duplex communication
  • what do you mean by lower latency interaction
like image 785
JR Sahoo.'JS' Avatar asked Jul 04 '17 06:07

JR Sahoo.'JS'


People also ask

What is the advantage of WebSocket?

WebSockets allow for a higher amount of efficiency compared to REST because they do not require the HTTP request/response overhead for each message sent and received. When a client wants ongoing updates about the state of the resource, WebSockets are generally a good fit.

Why WebSocket is closed?

The WebSocket is closed before the connection is established error message indicates that some client code, or other mechanism, has closed the websocket connection before the connection was fully established.

What is WebSocket and why is it important?

WebSocket is a two-way computer communication protocol over a single TCP. This is a common definition for it. Using WebSockets is a good way to handle high scale data transfers between server-clients. You can find many definitions like that on the simple Google (or Bing :)) search.

Do we need to close WebSocket?

If you are writing a server, you should make sure to send a close frame when the server closes a client connection. The normal TCP socket close method can sometimes be slow and cause applications to think the connection is still open even when it's not.


1 Answers

Why use websocket over http?

A webSocket is a continuous connection between client and server. That continuous connection allows the following:

  1. Data can be sent from server to client at any time, without the client even requesting it. This is often called server-push and is very valuable for applications where the client needs to know fairly quickly when something happens on the server (like a new chat messages has been received or a new price has been udpated). A client cannot be pushed data over http. The client would have to regularly poll by making an http request every few seconds in order to get timely new data. Client polling is not efficient.

  2. Data can be sent either way very efficiently. Because the connection is already established and a webSocket data frame is very efficiently organized (mostly 6 extra bytes, 2 bytes for header and 4 bytes for Mask), one can send data a lot more efficiently than via a HTTP request that necessarily contains headers, cookies etc...

what is full duplex communication?

Full duplex means that data can be sent either way on the connection at any time.

what do you mean by lower latency interaction

Low latency means that there is very little delay between the time you request something and the time you get a response. As it applies to webSockets, it just means that data can be sent quicker (particularly over slow links) because the connection has already been established so no extra packet roundtrips are required to establish the TCP connection.

For a comparison in what's involved to send some data via an http request vs. an already established webSocket connection see the steps listed in this answer: websocket vs rest API for real time data?

These other references may also be useful:

Server-push whenever a function is called: Ajax or WebSockets

For a push notification, is a websocket mandatory?

HTML5 WebSocket: A Quantum Leap in Scalability for the Web

like image 93
jfriend00 Avatar answered Sep 19 '22 10:09

jfriend00