What is WebSocket?

A communication method that keeps one connection open between a browser and a server so that either side can send messages at any time. The browser asks for Upgrade: websocket in an HTTP request, and when the server returns 101 Switching Protocols, the same TCP connection switches to WebSocket communication. URLs use the schemes ws and, protected by TLS, wss. For uses that need new information from the server delivered at once, such as chat, collaborative editing and price displays, it reduces traffic and delay compared with asking the server at fixed intervals.

Japanese name: WebSocket

WebSocket appears in 1 practice question across 1 topic, including Web Technology Basics.

Field: Web Technology Basics

Try it: questions about WebSocket

A chat app needs to deliver new messages from the server to the browser immediately. Which of the following correctly describes WebSocket?

  1. AA method in which the browser sends an HTTP request every few seconds and asks the server each time whether new messages have arrived
  2. BIt switches a connection started with HTTP (an upgrade) so that either the server or the browser can send messages over that one connection
  3. CA method in which, when an event occurs, a server notifies another server's pre-registered URL with an HTTP POST request
Show answer and explanation

Answer: B. It switches a connection started with HTTP (an upgrade) so that either the server or the browser can send messages over that one connection

WebSocket asks for Upgrade: websocket in an HTTP request, and when the server returns 101 Switching Protocols, the same TCP connection is switched to WebSocket communication. After that the connection is kept open, and either the server or the browser can send small messages with little overhead. With a method that asks at fixed intervals, requests occur even when there is nothing new, and new messages are delayed by up to the interval between checks. Notifying from server to server with an HTTP POST is called a webhook.

WebSocket in the explanations

Questions that mention WebSocket (1)

  1. A chat app needs to deliver new messages from the server to the browser immediately. Which of the following correctly describes WebSocket?

Books on Web Technology Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

HTTP (Hypertext Transfer Protocol) Cookie (HTTP cookie) CORS (cross-origin resource sharing) DOM (Document Object Model) Event loop HTTP method HTTP status code JSON (JavaScript Object Notation) JWT (JSON Web Token) OAuth

Back to the glossary