Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long polling netty nio framework java

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

like image 480
Alfred Avatar asked Feb 19 '10 04:02

Alfred


2 Answers

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);
like image 189
trustin Avatar answered Oct 02 '22 14:10

trustin


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.

like image 34
Nikita Koksharov Avatar answered Oct 02 '22 14:10

Nikita Koksharov