10

How can I do long-polling using netty framework? Say for example I fetch http://localhost/waitforx

but waitforx is asynchronous because it has to wait for an event? Say for example it fetches something from a blocking queue(can only fetch when data in queue). When getting item from queue I would like to sent data back to client. Hopefully somebody can give me some tips how to do this.

Many thanks

skaffman
  • 398,947
  • 96
  • 818
  • 769
Alfred
  • 60,935
  • 33
  • 147
  • 186

3 Answers3

10

You could write a response header first, and then send the body (content) later from other thread.

void messageReceived(...) {
    HttpResponse res = new DefaultHttpResponse(...);
    res.setHeader(...);
    ...
    channel.write(res);
}

// In a different thread..
ChannelBuffer partialContent = ...;
channel.write(partialContent);
trustin
  • 12,231
  • 6
  • 42
  • 52
  • Does this actually work? Can you write to a channel from another thread? It's thread-safe? – PP. May 08 '11 at 01:02
  • 1
    How does the "different thread" know which channel to use? For example, client1 does a long poll, do I have to put the channel in a HashMap and fetch it once the event has occurred so that I can send the partial content? – pkrish Aug 29 '12 at 14:45
3

You can use netty-socketio project. It's implementation of Socket.IO server with long polling support. On web side you can use Socket.IO client javascript lib.

Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71
0

You could also do the following in [sfnrpc]: http://code.google.com/p/sfnrpc

Object object = RPCClient.getInstance().invoke("#URN1","127.0.0.1:6878","echo",true,60,"", objArr,classArr, sl);

The true causes communication to be synchronous.

Paul Arer
  • 183
  • 3
  • 5