+5 votes
207 views
in Web development by (242k points)
reopened
What is WebSocket?

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

How does a web socket work?
What is WebSocket used for?
What advantages does WebSocket offer?
WebSocket - Useful Usage Examples
Where has WebSocket already been implemented?

image

What is WebSocket?

WebSocket is a TCP-based network protocol that establishes how data should be exchanged between networks. Since it is a reliable and efficient protocol, it is used by almost all clients. The TCP protocol establishes connections between two communication endpoints , called sockets . In this way, data exchange can take place in both directions..

For bi-directional connections, such as those created by WebSocket (sometimes also websocket or web socket ), data is exchanged in both directions at the same time. The advantage of this exchange is that the data is accessed faster . Specifically, WebSocket thus allows direct communication between a web application and a WebSocket server. In other words: the requested web is displayed in real time .

Index
  1. How does a web socket work?
  2. What is WebSocket used for?
  3. What advantages does WebSocket offer?
  4. WebSocket - Useful Usage Examples
  5. Where has WebSocket already been implemented?

How does a web socket work?

How do you access a web page without WebSocket? On the Internet, the transmission of web pages is usually done using an HTTP connection . This protocol is used to transmit data and makes it possible to load web pages in the browser. To achieve this, the client sends, with each user action (a click, for example), a request to the server.

To access a web page, in HTTP the client must first send a request to the server . Once submitted, the server can respond and display the requested content. It is a rigid request and response pattern that ultimately leads to long wait times between request and response..

image
HTTP connections are based on the classic question and answer scheme, in which the client must send a request to the server so that it can display the requested content.

The WebSocket protocol allowed for the first time to access a web dynamically in real time. With this protocol, it is enough for the client to establish a connection with the server, which is confirmed by the so-called handshake or WebSocket Protocol Handshake . With it, the client sends to the server all the identification data necessary for the exchange of information.

The communication channel remains, so to speak, open after the handshake . The server can activate itself and make all the information available to the client, without the client having to ask for it. Push notifications from web pages also work on this principle. If the server has new information, it communicates it to the client, without having to receive a specific request for it..

image
WebSocket can be understood as an open communication channel, in which an active connection remains open after the initial handshake between the client and the server. Thus, the server can also send new information to the client without the client having to request it each time.

To start the exchange, with WebSocket the client sends a request, just like in classic HTTP. However, the connection is established via TCP and remains open after the handshake between the client and the server, which has the following form:

The client sends the request:

  GET /chatService HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13  

The server responds:

  HTTP/1.1 101 Switching Protocols Upgrade: WebSocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: superchat  

The new WebSocket URL scheme for the displayed web pages is defined with the prefix ws instead of http . The prefix that corresponds to a secure connection is then wss , analogous to https .

What is WebSocket used for?

WebSocket is used whenever it is a question of establishing connections quickly . This is the case, for example, of technical support chats , news tickers or live stock updates, instant messaging services and real-time games. Conventional connection request patterns are no longer useful to meet the needs of many companies.

In social networks, WebSocket is also very useful: to establish live connections with other people, as well as to send and receive instant messages, WebSocket is always a good option, since it allows you to obtain high transmission speeds and limit latency times.

What advantages does WebSocket offer?

The traditional use of HTTP connections has the disadvantage that the client always loads the entire HTML page. To solve the problem, AJAX technology was developed. This had, for its part, the disadvantage of establishing unidirectional connections, that is, they only allow communication in one direction, which would lead to long waiting times in today's intensive applications, especially in chats. WebSocket, on the other hand, creates bidirectional connections that allow the exchange of data in both directions, which makes direct contact with the browser possible and, with this, allows short loading periods : as soon as a message is sent, such as one in a tech support chat, it comes in and is shown directly to the other side.

WebSocket - Useful Usage Examples

WebSocket is convenient for anyone who wants a fast Internet connection. Nowadays, there are many areas that require a real-time connection between the client and the server in order to offer their services without complications. Some of these fields of application are the following:

  • Online games
  • Buying and selling platforms, such as eBay
  • User service chats
  • Live sports news tickers
  • Real-time updates from social networks

WebSocket is not a total replacement for HTTP, but it can be used as an efficient two-way communication channel whenever you need to give or receive information in real time.

Where has WebSocket already been implemented?

To use WebSocket you need a current browser:

  • Internet Explorer from version 10
  • Firefox from version 6
  • Chrome from version 14
  • Opera from version 12.10
  • Safari from version 6

On the server side, WebSocket can be implemented with the following programming languages ​​and frameworks :

  • Node.js
    • Socket.IO
    • WebSocket-Node
    • ws
  • Java
    • Jetty
  • Ruby
    • EventMachine
  • Python
    • pyWebSocket
    • Twister
  • Erlang
    • Shirasu
  • C ++
    • libWebSockets
  • .NET
    • SuperWebSocket
In summary

The WebSocket protocol is a technology related to the development of HTML5: an attempt to make the web faster, more dynamic and more secure. This efficient protocol allows modern web applications to react much faster than they would based on conventional HTTP communication. However, this by no means means that the traditional protocol has to be replaced: despite the existence of WebSocket, HTTP remains a key standard on the Internet.


...