benstpierre
benstpierre

Reputation: 33591

What is ajax-push? Are there caveats to using it on some servers?

Can somebody explain what ajax-push is? From what I understand it involves leaving HTTP connections open for a long time and reconnecting as needed. It seems to be used in chat systems a lot.

I have also heard when using ajax-push in Java it is important to use something with the NIO-connetors or grizzle serlvet api? Again, I'm just researching what it exactly.

Upvotes: 1

Views: 970

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340953

In normal AJAX (call it pull) you ask the server for something and you get it immediately. This is fine when you want to get some data from the server now. But what if something happens on the server and the server wants to push that event to the client(s)?

Technically this is implemented using so called long polling - the browser opens the HTTP connection and waits for the response. As long as there is nothing interesting on the server side, it waits. But when something happens, the server sends the response and the client receives it immediately. This is a huge advantage over normal polling where you ask the server every few seconds - it generates a lot of traffic and still introduces noticeable latency.

The only problem with this approach is the number of pending HTTP connections. Old-school Java servlet containers aren't quite capable of handling such amount of connections due to one-thread-per-connection limitation - they quickly run out of memory. Even though the HTTP threads aren't doing anything (waiting for some other part of the system to wake them up and give them the response), they occupy memory.

However there are plenty of solutions nowadays:

  • Tomcat NIO connectors

  • Atmosphere Ajax Push/Comet library

  • Servlet 3.0 @Async (most portable)

  • Container-specific features, but Servlet 3.0, if available, should be considered superior.

Upvotes: 4

Related Questions