Skip to content Skip to sidebar Skip to footer

How Can I Wait For A Socket.io Connection To Return Data Using Casperjs?

I am scraping a page which uses socket.io to populate some select tag options. How can I wait for the socket to receive data before evaluating the page? I am using casperJS the soc

Solution 1:

You could do something like the following as lud1977 describes I guess, although I don't like it. Casper's waitFor works by polling, while that's really stupid when waiting for an event to happen. It would be way better if you do this:

var list = null; // broader scope

casper.on('socket.list', function() {
  // do stuff with <list> here
});

casper.waitForResource("socket.io.js", function() { 
  var socket = io.connect("http://wherever-the-server-is")
  socket.on('list', function(data){
    list = data;
    this.emit('socket.list');
  }.bind(this));
})

Source: http://docs.casperjs.org/en/latest/events-filters.html

Solution 2:

i suggest you take a look at this page: how to wait for element visibility in phantomjs

particularly the waitfor function. In the situation you described, I would use something like that to check if the page has been populated with data from the socket, meaning the socket has finished loading.

Hope it helps.

Post a Comment for "How Can I Wait For A Socket.io Connection To Return Data Using Casperjs?"