Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Update</h2> <p>As I continue to recieve upvotes on this, I think it is reasonable to remember that this answer is 4 years old. Web has grown in a really fast pace, so please be mindful about this answer.</p> <hr> <p>I had the same issue recently and researched about the subject.</p> <p>The solution given is called long polling, and to correctly use it you must be sure that your AJAX request has a "large" timeout and to always make this request after the current ends (timeout, error or success).</p> <h2>Long Polling - Client</h2> <p>Here, to keep code short, I will use jQuery: </p> <pre><code>function pollTask() { $.ajax({ url: '/api/Polling', async: true, // by default, it's async, but... dataType: 'json', // or the dataType you are working with timeout: 10000, // IMPORTANT! this is a 10 seconds timeout cache: false }).done(function (eventList) { // Handle your data here var data; for (var eventName in eventList) { data = eventList[eventName]; dispatcher.handle(eventName, data); // handle the `eventName` with `data` } }).always(pollTask); } </code></pre> <p>It is important to remember that (from <a href="https://api.jquery.com/jquery.ajax/" rel="nofollow noreferrer">jQuery docs</a>): </p> <blockquote> <p>In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.</p> </blockquote> <h2>Long Polling - Server</h2> <p>It is not in any specific language, but it would be something like this:</p> <pre><code>function handleRequest () { while (!anythingHappened() || hasTimedOut()) { sleep(2); } return events(); } </code></pre> <p>Here, <code>hasTimedOut</code> will make sure your code does not wait forever, and <code>anythingHappened</code>, will check if any event happend. The <code>sleep</code> is for releasing your thread to do other stuff while nothing happens. The <code>events</code> will return a dictionary of events (or any other data structure you may prefer) in JSON format (or any other you prefer).</p> <p>It surely solves the problem, but, if you are concerned about scalability and perfomance as I was when researching, you might consider another solution I found.</p> <h2>Solution</h2> <p>Use sockets! </p> <p>On client side, to avoid any compatibility issues, use <a href="https://socket.io" rel="nofollow noreferrer">socket.io</a>. It tries to use socket directly, and have fallbacks to other solutions when sockets are not available.</p> <p>On server side, create a server using NodeJS (example <a href="http://www.stoimen.com/blog/2010/12/02/diving-into-node-js-a-long-polling-example/" rel="nofollow noreferrer">here</a>). The client will subscribe to this channel (observer) created with the server. Whenever a notification has to be sent, it is published in this channel and the subscriptor (client) gets notified.</p> <p>If you don't like this solution, try APE (<a href="http://www.ape-project.org/" rel="nofollow noreferrer">Ajax Push Engine</a>).</p> <p>Hope I helped.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload