The WebSocket protocol is a TCP-based network protocol. It defines how data is exchanged between networks. Because it is very reliable and efficient, it is used by almost all clients. TCP es­tab­lish­es com­mu­ni­ca­tion between two endpoints, which are referred to as sockets. This allows data to be trans­ferred in both di­rec­tions.

Two-way con­nec­tions like WebSocket (sometimes written as Web Socket) allow data to be exchanged in both di­rec­tions si­mul­ta­ne­ous­ly. The advantage of this is that data can be called up more quickly. WebSocket in par­tic­u­lar enables direct com­mu­ni­ca­tion between a web ap­pli­ca­tion and a WebSocket server. In concrete terms, this means you can call up a web page and have it displayed in “real time.”

How does WebSocket work?

Firstly, let’s look at how websites are called up without WebSocket. On the Internet, web pages are usually trans­ferred via a HTTP con­nec­tion. Data is com­mu­ni­cat­ed via the protocol so that the website can be displayed in your browser. For this to happen, for every action you make (e.g. a click), the client sends a request to the server.

When a website is called up via HTTP, the client first has to send a request to the server. The server then responds by sending the requested content. In other words, HTTP works based on a simple request-and-response model, which generates a sig­nif­i­cant delay.

With the WebSocket protocol things are different. Thanks to this tech­nol­o­gy, websites can be called up in real-time using a dynamic call-up procedure. Here, all the client needs to do is open the con­nec­tion to the server. It does this by sending the WebSocket protocol handshake. The handshake contains all of the iden­ti­fi­ca­tion in­for­ma­tion required for data exchange.

After the handshake, the channel is kept open, allowing for almost con­tin­u­ous com­mu­ni­ca­tion. The server can in­de­pen­dent­ly send data to the client without the client having to request it. Push no­ti­fi­ca­tions on websites use this principle. If the server has new in­for­ma­tion, it sends this to the client without any need for a specific request from the client side.

To initiate com­mu­ni­ca­tion, the client sends a request just like with HTTP, but after this, an open con­nec­tion is main­tained via TCP. The content of the handshake between the client and the server is as follows:

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 answers:

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

The new URL scheme for websites that use WebSocket is ws instead of http. There is also an equiv­a­lent for secure con­nec­tions: wss instead of https.

When should you use WebSocket?

WebSocket is useful when fast con­nec­tions are important. Examples include live chats on support websites, news tickers, stock tickers, messaging apps, and real-time games. For most companies today, standard con­nec­tion requests are no longer suf­fi­cient.

Social media sites can benefit from WebSocket too, because it allows live in­ter­ac­tion and instant messaging between users. WebSocket is a logical choice for any ap­pli­ca­tion that requires a high-speed, low-latency con­nec­tion.

What are the ad­van­tages of WebSocket?

With con­ven­tion­al HTTP con­nec­tions, problems may arise because the client always loads the entire HTML page. AJAX tech­nol­o­gy was developed to address this issue. However, this still wasn’t a perfect solution, because it only allows one-way com­mu­ni­ca­tion, which leads to con­sid­er­able delays that are no longer com­pat­i­ble with today’s ap­pli­ca­tions, in par­tic­u­lar live chats. By es­tab­lish­ing a two-way data con­nec­tion, WebSocket enables direct contact with the browser, which reduces loading times. In a live support chat, this means messages are displayed instantly.

Good examples of WebSocket in practice

WebSocket is suitable whenever fast online con­nec­tions are required. Nowadays, many sites rely on real-time con­nec­tions to offer certain services. These include:

  • Online games
  • Sales platforms like eBay
  • Support chats
  • Live sport tickers
  • Real-time updates on social media

WebSocket is not a direct sub­sti­tute for HTTP, but it allows efficient two-way com­mu­ni­ca­tion and is therefore very useful when real-time display is required.

What support currently exists for WebSocket?

WebSocket is currently supported by the following browsers:

  • Internet Explorer: version 10 or later
  • Firefox: version 6 or later
  • Chrome: version 14 or later
  • Opera: version 12.10 or later
  • Safari: version 6 or later

On the server side, WebSocket can be used with the following pro­gram­ming languages and frame­works:

  • Node.js
    • Socket.IO
    • WebSocket-Node
    • ws
  • Java
    • Jetty
  • Ruby
    • Event­Ma­chine
  • Python
    • py­Web­Sock­et
    • Tornado
  • Erlang
    • Shirasu
  • C++
    • Lib­web­sock­ets
  • .NET
    • Su­per­Web­Sock­et
Con­clu­sion

The WebSocket tech­nol­o­gy is aligned with the de­vel­op­ment of HTML5 in that it aims to make the web faster, safer, and more dynamic. It is an efficient protocol that is in­valu­able for modern web ap­pli­ca­tions, which require much faster in­ter­ac­tions than can be achieved with a con­ven­tion­al HTTP con­nec­tion. However, the older protocol should by no means be abolished. Despite the existence of WebSocket, HTTP remains an important internet standard.

Go to Main Menu