Simulate longer response from server in Meteor.js

03-10-2014 (d-m-Y)

When I develop an app on localhost, everything is ok. Until I deploy it. I usually see longer responses from server and user interface seems to be frozen for a while. To simulate longer response on localhost you can use Meteor._sleepForMs():

//server
Meteor.methods({
  'yourMethod': function() {
    Meteor._sleepForMs(2000); //to simulate longer response sleep for 2 seconds
    //do something    
    return 'something';
  }
});

Don't forget to delete Meteor._sleepForMs() before deploy ;-).

Or if you often forget, you can check if the code runs on localhost:

//server
Meteor.methods({
  'yourMethod': function() {
    if (/localhost/.test(Meteor.absoluteUrl())) {
      Meteor._sleepForMs(2000); //to simulate longer response sleep for 2 seconds only on localhost
    }
    //do something    
    return 'something';
  }
});

Enjoy.

EDIT: You can use latency compensation if you don't want to wait for the server response.

← All articles | If you want to know about new blogposts you should follow @elfoslav

comments powered by Disqus