WebSockets ¶
SSE¶
According to the definition, it is a server push technology enabling a client to receive automatic updates from a server. Using SSE, the clients make a persistent long-term connection with the server. Then, the server uses this connection to send the data to the client.
Client can't send request twice
It is unidirectional, meaning once the client sends the request it can only receive the responses without the ability to send new requests over the same connection.
- The client makes a request to the server.
- The connection between client and server is established, and it remains open.
- The server sends responses or events to the client when new data is available.
WebSockets¶
WebSocket provides full-duplex communication
channels over a single TCP connection. It is a persistent connection between a client and a server that both parties can use to start sending data at any time.
How this connection works?
The client establishes a WebSocket connection through a process known as the WebSocket handshake
. If the process succeeds, then the server and client can exchange data in both directions at any time. The WebSocket protocol enables the communication between a client and a server with lower overheads, facilitating real-time data transfer
from-and-to the server.
- The client initiates a WebSocket handshake process by sending a request.
- The request also contains an HTTP Upgrade header that allows the request to switch to the WebSocket protocol
ws://
- The server sends a response to the client, acknowledging the WebSocket handshake request.
- A WebSocket connection will be opened once the client receives a successful handshake response.
- Now the client and server can start sending data in both directions allowing real-time communication.
- The connection is closed once the server or the client decides to close the connection.
Example