Sample code for 30+ languages & platforms
JavaScript

Check for IMAP IDLE Mailbox Updates

See more IMAP Examples

Demonstrates the Chilkat Imap.IdleCheck method, which waits for mailbox updates after IdleStart has entered IDLE mode. The only argument is a timeout in milliseconds; 0 performs a non-blocking poll of notifications already received. This example waits up to 30 seconds.

Background: IdleCheck consumes the notifications already waiting on the connection rather than sending a new polling command. A typical monitoring loop repeatedly calls IdleCheck with a timeout and reacts when updates arrive. Because servers usually drop an idle connection after about 29 minutes, long-lived monitors periodically call IdleDone and then IdleStart again to refresh it.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

//  Demonstrates the Imap.IdleCheck method, which waits for IMAP IDLE mailbox updates and returns
//  them.  The only argument is a timeout in milliseconds.  Use 0 for a non-blocking poll.

var imap = new CkImap();

imap.Ssl = true;
imap.Port = 993;

success = imap.Connect("imap.example.com");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.Login("user@example.com","myPassword");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.SelectMailbox("Inbox");
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.IdleStart();
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

//  Wait up to 30 seconds for mailbox updates.  IdleCheck returns a string, so assign it to a
//  string variable and check for success.
var timeoutMs = 30000;
var updates = imap.IdleCheck(timeoutMs);
if (imap.LastMethodSuccess == false) {
    console.log(imap.LastErrorText);
    return;
}

console.log(updates);

success = imap.IdleDone();
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}

success = imap.Disconnect();
if (success == false) {
    console.log(imap.LastErrorText);
    return;
}