Part 2- What is WebSocket? Polling vs Server Sent Events vs WebSocket

What have we learned so far,
Part 1 – How server sends notification to client? Polling and Server Sent Events
https://www.onlyfullstack.com/polling-and-server-sent-events/

What is WebSocket?

The WebSocket protocol enables interaction between a web browser (or other client application) and a web server with lower overheads, facilitating real-time data transfer from and to the server. This is made possible by providing a standardized way for the server to send content to the client without being first requested by the client, and allowing messages to be passed back and forth while keeping the connection open.

Why WebSocket?
WebSockets enable the server and client to send messages to each other at any time, after a connection is established, without an explicit request by one or the other. This is in contrast to HTTP, which is traditionally associated with the challenge-response principle — where to get data one has to explicitly request it. In more technical terms, WebSockets enable a full-duplex connection between the client and the server.

In a challenge-response system there is no way for clients to know when new data is available for them (except by asking the server periodically —polling or long polling), with Websockets the server can push new data at any time which makes them the better candidate for “real-time” applications.

Polling vs Server Sent Events vs WebSocket
1. Message Flow
Polling : 

Polling

Server Sent Events : 

SSE


WebSockets : 

websocket




2. Communication Techniques
Polling : request → response. Creates a connection to the server, sends request headers with optional data, gets a response from the server, and closes the connection. Supported in all major browsers.

Server Sent Events : request → wait → response. Creates a connection to the server, but maintains a keep-alive connection open for some time (not long though). During connection, the open client can receive data from the server.

WebSockets : client ↔ server. Create a TCP connection to the server, and keep it open as long as needed. The server or client can easily close the connection. The client goes through an HTTP compatible handshake process. If it succeeds, then the server and client can exchange data in both directions at any time.

3. Communication Type
Polling : Supports unidirectional communication where only server can send the data on clients request.

Server Sent Events : Supports unidirectional communication where only server can send the data to client

WebSocket : Supports bidirectional communication where server as well as client can send the data over the network

4. Protocol
Polling : Uses HTTP protocol for the communication.

SSE : Uses HTTP protocol for the communication.
WebSocket : Uses websocket protocol for the communication.

5. Usage
Polling : Avoid polling as its an old technique to get the data from the server.

Server Sent Events : SSE can be used when we only want to send the data from server to client. So the example of SSE would be –
    – A real-time chart of streaming stock prices
– Real-time news coverage of an important event (posting links, tweets, and images)
– A live Github / Twitter dashboard wall fed by Twitter’s streaming API
– A monitor for server statistics like uptime, health, and running processes.

WebSocket : WebSockets can be used when we need bidirectional communication in between client and server. Some of examples are as below –
    – Chat applications
    – Displaying real-time market news
    – Market data

Source Code
Download the source code of WebSocket tutorial from below git repository :
websocket-with-spring-boot-and-angular-6

Let’s go to our next tutorial where we will discuss below points :
Part 3 – WebSocket Example with Spring Boot + Angular 6 + STOMP

In this tutorial, we will understand below topics –

    – Spring Boot + STOMP + WebSocket Backend
    – Angular 6 + StompJs + WebSocket Frontend

WebSocket Tutorial