Adam Dawkins
Adam Dawkins

Reputation: 51

How do I stop browser from 'waiting' for Node.js callback

I am creating an Express JS app on Node.js and have implemented a page that will callback for live updates as per chat.nodejs.org (https://github.com/ry/node_chat). I've followed the approach in 'node_chat' to a t, and the page updates correctly if an update is made from another browser. The only problem is I can't seem to stop the browser (Chrome) from 'Waiting for localhost' and spinning away, which I don't think it needs to do.

Here's simplified versions of my code (for brevity). Any ideas?

client.js (client side):

function poll(data) {
// ...
if(data){
    // do something with the data

} 
    $.ajax({
        cache:false,
        type:'GET',
        url:'/json-people',
        dataType: 'json',
        data: {
            slug: queue.slug,
            since: CONFIG.last_timestamp
            },
        error: function () {
            // handle error
        },
        success: function (data) {
            poll(data);
        }
    });
};

app.js:

app.get('/json-people', function (req, res) { 
  queueProvider.query(req.param('slug'), req.param('since'), function (people){
    res.send({people: people}, 200);
  });

});

it doesn't seem to matter what I put in the queueProvider.query method, it will spin away until res.send(...) is executed.

I used https://github.com/ry/node_chat/blob/master/client.js and https://github.com/ry/node_chat/blob/master/server.js for reference.

Particularily:

fu.get("/recv", function (req, res) {...}

and

channel.query()

and

channel.appendMessage()

Thanks.

Upvotes: 4

Views: 1280

Answers (1)

nicolaskruchten
nicolaskruchten

Reputation: 27410

I would recommend that to avoid this spinning icon, you switch from polling to something like WebSockets (e.g. http://socket.io)

Upvotes: 5

Related Questions