1

I need to implement long polling or pending request for a chatroom. I tried a4j:push, but it seems doesn't work like a real long polling approach (see the following discussion: https://community.jboss.org/message/16614).

The question is: which alternatives do I have to realize long polling?

I'm using JSF 1.2, JAVA EE 6 and RichFaces 3.3.2.

Thaks in advance!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
golinko
  • 327
  • 2
  • 3
  • 14

2 Answers2

1

You need to use the a4j:poll component from RichFaces. The exadel live demo has a very nice sample and explains the main properties. Plus, you can get more info in the official documentation.

Maybe you want to look at a chat implementation example and not polling. There is a question about it:

https://stackoverflow.com/a/1577486/1065197

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Hi, thanks for the answer. It must be a misunderstanding, under long polling I meant a method of implementing server push over http, in which the client makes a request of a resource on the server, and the server does not respond, nor close the connection until there is data to return or a specified time elapses. After the server responds or closes the connection, the client waits some amount of time and then reconnects, and the process repeats. – golinko Mar 11 '12 at 19:05
  • 1
    And as I understood a4j:poll and a4j:push send periodically requests to server (say each second) and this could be crucial for the server in case of hundreds of clients... – golinko Mar 11 '12 at 19:14
  • The `a4j:poll` tag component does that and you can configure how much time the client must wait to ask the server if there is any result to display. The live exadel demo shows that the clients polls the server every 0.5 secs to perform a "display date and time" action. – Luiggi Mendoza Mar 11 '12 at 19:53
0

Try to use netty-socketio java project. It has long polling support. Use Socket.IO client javascript lib on your jsf page.

Javascript lib usage example:

<script type="text/javascript">
    var socket = io.connect('http://localhost:81', {
      'transports' : [ 'xhr-polling' ],
      'reconnection delay' : 2000,
      'force new connection' : true
    });
    socket.on('message', function(data) {
         // here is your handler on messages from server
    });

    // send object to server
    var obj = ...
    socket.json.send(obj);
</script>
Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71