Reverse AJAX - Old school

Reverse Ajax is essentially a concept: being able to send data from the server to the client.

There are various ways to achieve this

1. HTTP polling

Polling involves issuing a request from the client to the server to ask for some data.
This is obviously a mere Ajax HTTP request. To get the server events as soon as possible, the polling interval (time between requests) must be as low as possible.
There's a drawback: if this interval is reduced, the client browser is going to issue many more requests, many of which won't return any useful data, and will consume bandwidth and processing resources for nothing.

Advantages: It's really easy to implement and does not require any special features on the server side. It also works in all browsers.
Disadvantage: This method is rarely employed because it does not scale at all.

There are many things to note here.
- Since tomcat 8 uses NIO connector by default it uses threads from a pool and that's evident when you notice the thread names for different requests
- Note that in the example above client keeps polling infinitely and the server keeps queuing infinitely. Both shouldn't happen and should be handled in real
scenarios.

2. Piggyback

Piggyback polling is a much more clever method than polling since it tends to remove all non-needed requests (those returning no data).
There is no interval; requests are sent when the client needs to send a request to the server. The difference lies in the response, which is split into two parts: the response for the requested data and the server events if any occurred.

Advantages: With no requests returning no data since the client controls when it sends requests, you have less resource consumption. It also works in all browsers and does not require special features on the server side.
Disadvantage: You have no clue when the events accumulated on the server side will be delivered to the client because it requires a client action to request them.