# HTTP vs WebSocket

|                             | **HTTP**                                                                                                                                                                                                                                                                                                          | **WebSocket**                                                                                                                                                                                                                                                                                                                                                                      |
| --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Data transmission mechanism | It is a request-response protocol, meaning that when a client sends a request to the server, the server will return a single and brief response. After that, the connection between the client and server will close. This means that to send new data, the client will have to send a new request to the server. | It is a full-duplex protocol, meaning both the client and server can send and receive data simultaneously without having to send requests and responses one after another like HTTP. The connection between the client and server will be kept open throughout the entire session, allowing data to be sent and received directly without the need to re-establish the connection. |
| Establish connection        | The connection is established by sending a request from the client to the server. After the server returns a response, the connection between the client and server is closed.                                                                                                                                    | The connection is established using a separate protocol (WebSocket handshake protocol) to establish the connection between the client and the server. Once the connection is established, the client and server send data to each other without sending requests and responses.                                                                                                    |
| Data format                 | Typically uses data formats such as HTML, JSON or XML to transmit data between client and server.                                                                                                                                                                                                                 | There is no limit to the data format used to transmit data between client and server.                                                                                                                                                                                                                                                                                              |
| Reliability                 | The reliability of HTTP depends on the protocol used to send data (e.g. TCP or UDP). If the network connection fails or is interrupted, data transmission over HTTP may be lost or corrupted.                                                                                                                     | WebSocket is designed to minimize connection loss or interruption                                                                                                                                                                                                                                                                                                                  |
| Maintain status             | Stateless                                                                                                                                                                                                                                                                                                         | Stateful                                                                                                                                                                                                                                                                                                                                                                           |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huy312100.gitbook.io/software-development/everything-anyone-should-know/fundamental/api-styles/comparation/http-vs-websocket.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
