How to sleep while listening for agent message?

I adapted the SnackBot example to send my own messages from an agent generated web page, but when I use the imp.onidle method below the device never receives the agent message. If I comment out the following code then the agent.on method is processed (although the delay from clicking a button to receiving the message can be 30+ seconds).

imp.onidle(function() { do_stuff(); server.sleepfor(20); });

The device probably cannot sleep while listening for agent messages. If that is true how do I do_stuff() on an interval? In the SnackBot example, does the device respond immediately to a button click?

server.sleepfor() puts the imp into a deep sleep mode - which you don’t want if you would like to continue processing messages.

The typical way is to create a loop with imp.wakeup():

`function loop() {
imp.wakeup(20, loop);

loop(); // call loop function once to get it started`