Search code examples
c#javascriptsystem.reactivelong-pollingofflineapps

How to detect if a client is subscribed to receive push-based notifications when using long polling


Say you have a long polling situation with a server and a client that subscribes to notifications using ajax recursively,

var subscribe = function () {
$.get('http://localhost:1234', function (data) {
 // use the data                                
 subscribe(); // send another request
                });
            }

On the server side, is there a reliable way to detect whether the client is actually there to receive requests -- in other words, that someone has the page open and is ready to receive pushed data? How do you handle the fact that clients might be offline but need to receive the data they missed upon logging back in?

I am currently implementing the server using reactive extensions for .NET based on the following link,

http://joseoncode.com/2011/06/17/event-driven-http-server-in-c-with-rx-and-httplistener/

 using (var server = new HttpServer("http://localhost:1234/"))
        {
            //the listeners stream and subscription
            var listeners = server
                    .Where(ctx => ctx.Request.HttpMethod == "GET")
                     //wait the next message to end the request
                    .Subscribe(ctx => subject.Take(1)                                                  .Subscribe(m => ctx.Respond(new StringResponse(m))));  

Solution

  • On the server side, is there a reliable way to detect whether the client is actually there to receive requests -- in other words, that someone has the page open and is ready to receive pushed data?

    I think the reliable way is to just wait for the client to request notifications from the server.

    The client request will need to have a unique client id that the server stores for those client that successfully subscribe. If the id is there then give him what he wants.

    How do you handle the fact that clients might be offline but need to receive the data they missed upon logging back in?

    How long will the client stay offline and how often are the new notifications created? If client stayed offline too long then she'll receive a lot of notifications by then.

    Or maybe you mean client subscribes then loses her internet connection. Then each client will need a queue where notifications stay before client requests for them. While client stays offline, messages stay in the queue. You'll probably need a decay time for the messages in queue to get cleared if client stays offline too long so storage doesn't get big (assuming you'll use redis or something similar).