Web Technology Basics
A chat app needs to deliver new messages from the server to the browser immediately. Which of the following correctly describes WebSocket?
- AA method in which the browser sends an HTTP request every few seconds and asks the server each time whether new messages have arrived
- BIt switches a connection started with HTTP (an upgrade) so that either the server or the browser can send messages over that one connection
- 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.
- A. This is a method of asking periodically (polling). Requests occur even when there is nothing new, and delays of up to the interval arise.
- B. Correct. The connection stays open, and messages can be sent in both directions at any time.
- C. This describes a webhook. It is used for notifications between servers.