What is WebSocket?

2020-11-09 15:20:52
Blog picture

Recently, we published a blog post describing why WebSockets are great for real-time services. In this article, we describe the process of establishing, maintaining and closing the WebSockets connection. 

WebSocket is a protocol that provides low-latency data exchange between client and server. The WebSocket protocol maintains persistent two-way communication between parties. This means that a client can receive and send data to the server at any time during the session and vice-versa. In contrast to the HTTP protocol, no extra requests are required.

The WebSocket protocol is great for services that require real-time communications such as natural language processing and chats.

WebSocket Connection

The WebSocket protocol workflow consists of two stages: opening handshake and data transfer. These parts are logically separated from each other. 

Opening Handshake

To establish a new WebSocket connection, parties use the request/response HTTP approach. The connection starts when the client sends a request to the browser. This is what it looks like:


Host: www.voximplant.com

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key: Iv7io/9s+lYFgYBcXczP8Q==

Origin: www.voximplant.com

Sec-WebSocket-Version: 13
  • Upgrade: websocket – describes switching to the WebSocket protocol 
  • Sec-WebSocket-Key: Iv7io/9s+lYFgYBcXczP8Q== – a 16-bit randomly generated code used to protect against fake requests
  • Sec-WebSocket-Version: 13 – provides the current WebSockets version
  • Origin – the origin of the client page

The server also responds in an HTTP-like way:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: hsBlbuDTkk24srzEOTBUlZAlC2g=
  • HTTP/1.1 101 Switching Protocols – a protocol switching code
  • Sec-WebSocket-Accept: hsBlbuDTkk24srzEOTBUlZAlC2g= – a security key which is generated based on the client key using the RFC 6455 algorithm

After that, HTTP is no longer used. WebSockets has established a TCP-based persistent connection for data transfers.

Data Transfer

In WebSockets, data is transferred in frames – data fragments – using the TCP connection. To minimize buffering on large data transfers, WebSockets fragments the message into several small frames.

WebSockets support three types of frames:

  1. Text frames. Contain UTF-8 encoded text data that parties send to each other
  2. Binary data frames. Contain binary data 
  3. Control frames. Used to maintain and close the connection

How Connections are Maintained

To avoid connection timeouts and prevent proxies from closing connections, WebSockets can send ‘ping/pong’ requests. The server periodically sends a 'ping' control frame and the client responds with a 'pong' control frame.

The ping/pong approach resolves two problems:

  1. It makes sure the second party is still functioning
  2. It maintains a TCP connection if the client and server are not directly connected, but through a proxy server

Connection Close

To close the WebSocket connection, parties exchange the ‘close’ type of control frames. Either the server or the client can close the connection. 

The client can send the ‘close’ request to the server. In turn, the server sends the same request and breaks the connection.

Try WebSockets with Voximplant

Voximplant simplifies app development with the WebSocket API that is located in the cloud. When your app calls a third-party service, our cloud opens the WebSockets connection and sends and receives data. You can build and scale your real-time app without the need to provision servers or monitor capacity utilization.

Here are the instructions you can use to create inbound and outbound WebSockets connections. For instance, you can add speech synthesis and recognition to your bots using the WebSocket API.

Create a developer account and try out our platform for free.



Sign Up for a free Voximplant developer account or talk to our experts

Add your comment


Your comment has been added and will be published after moderation.

Recommended posts

Speech Technologies For Your Business – The Future of CX

Speech Technologies For Your Business – The Future of CX

Striving to offer the best service possible, contact centers are usually very quick to adopt new technologies and try out innovative solutions. Successful implementation of speech technologies can create a great competitive advantage for a company.

Personalized Service 101: What it is and How to Deliver it

Personalized Service 101: What it is and How to Deliver it

For many consumers, personalized customer service is key to good customer experiences (CX). According to a 2020 survey by Gladly, close to two-thirds of respondents said that “lack of personalization makes [them] feel like a ticket number.” Businesses wishing to meet and exceed customer expectations need to make personalized service a top priority. But personalization goes beyond making movie recommendations or using a customer’s name in an email subject line. When it comes to personalized service, it’s paramount that you deliver relevant, valuable, and speedy support to promote positive customer experiences. In this article, we’ll break down what personalized service means, why it’s so important to CX, and how you can deliver personalization service to your customers.

5 Tactics to Strengthen Your Contact Center Operations with Technology

5 Tactics to Strengthen Your Contact Center Operations with Technology

In our fast-paced, digitally focused world, customers want personalized service at lightning speed. A Hubspot survey of 1,000 customers found that 90% of people expect a response from a live customer service agent in ten minutes or less. Because expectations are so high, the pressure is on for customer service agents to consistently provide quick, high-quality customer experiences. Offering an elevated level of service with such a fast turnaround is next to impossible with manual support alone. And even if you have some digital tools in your tech stack, traditional contact center technology can fall short when it comes to efficiency. That’s why choosing no-fuss technology that automates your contact center’s routine operations is essential for making tasks more manageable for your agents.