The client initiating long polling, calls a method on server and passes in an instance of AsyncCallback
which contains the callback delegate which will be invoked when server asynchronously gets back to the client.
Now my understanding on this is limited but it appears that in BasicHttp
WCF the AsyncCallback
parameter is serialised and sent to server which then de-serialises it, caches it and ultimately invokes it to "get back" to the client.
Firstly, is the above explanation correct? Secondly, how does the AsyncCallback
invoked on a client all the way across network?
In a Nutshell: HTTP Long Polling To overcome this deficiency, Web app developers can implement a technique called HTTP long polling, where the client polls the server requesting new information. The server holds the request open until new data is available. Once available, the server responds and sends the new information.
Long polling. So-called “long polling” is a much better way to poll the server. It’s also very easy to implement, and delivers messages without delays. The flow: A request is sent to the server. The server doesn’t close the connection until it has a message to send. When a message appears – the server responds to the request with it.
There’s always a client request at the core of any solution based on polling. The request stands until the server has replied. Any pending request consumes a browser connection and, more importantly, engages a server thread, making that valuable resource unavailable to other requests.
The first completed in two minutes; the second is still pending. This is the essence of long polling—the client has a channel continuously open with the server. The server times out the request after two minutes if no action is performed on the server that requires sending data back to the client.
The connection is kept open so the server responds over the existing connection, including the callback handler name in the response.
The client understands the format of the message and can then invoke the appropriate local method (based on the callback handler) with the data from the server response.
I usually prefer not to quote Wikipedia but in this instance, it's not a bad explanation of long polling...
Long polling is a variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll. However, if the server does not have any information available for the client, instead of sending an empty response, the server holds the request and waits for some information to be available. Once the information becomes available (or after a suitable timeout), a complete response is sent to the client. The client will normally then immediately re-request information from the server, so that the server will almost always have an available waiting request that it can use to deliver data in response to an event. In a web/AJAX context, long polling is also known as Comet programming.
AsyncCallback
's methods on the server)This is similar to the way JSONP works (The callback part, not long polling), if you're familiar with that? Essentially, the Callback handle is only passed to the server so that it can be sent back with the response and allow the client to call the correct method.
There are additional checks going on under the hood to make sure that only the intended methods are called and a malicious server can't just execute any method it chooses in client code.
@Basic's answer is very good, but I'm feeling like writing a description that hopefully is easier to understand for some people.
AsyncCallback
passed inAsyncCallback
is called with the informationSo it really the framework (on the client side still) waiting for the response that is the async part. The TCP connection and server processing is all standard (other than the waiting to respond in the case of nothing to return for long polling)
This should describe the basic process for any async web requests in any language, be it long polling, downloading images, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With