How IMAP idle works?
Asked Answered
S

2

4

Can someone explain to me how IMAP IDLE works? Does it fork a new process for each connection that it opens? Can I somehow use eventmachine with it?

I am trying to implement it in ruby on heroku with background workers. Any thoughts?

Superannuated answered 6/1, 2011 at 4:22 Comment(3)
Do you want to know what IMAP IDLE is and what data you'd need to send back to the client when, or do you want to know how to implement IMAP IDLE in your existing IMAP server?Mellicent
IMAP IDLE is a part of the IMAP protocol. So yes, what dkarp asked: Do you want to know what the protocol spec is, or how to implement it on a website (I'm guessing your not making an IMAP server since you're talking about heroku).Hormone
See here for an answer: https://mcmap.net/q/1204149/-support-for-imap-idle-in-rubyDuhl
A
10

In Ruby 2.0 and up, there's an idle method that accepts a code block that will be called every time you get an untagged response. Once you got this response, you need to break out and pull the emails that came in. The idle call is also blocking, so you need to do this in a thread if you want to keep it asynchronous.

Here's a sample (@mailbox is an instance of Net::IMAP in this case):

def start_listener()
    @idler_thread = Thread.new do
        # Run this forever. You can kill the thread when you're done. IMAP lib will send the 
        # DONE for you if it detects this thread terminating
        loop do
            begin
                @mailbox.select("INBOX")
                @mailbox.idle do |resp|
                    # You'll get all the things from the server. For new emails you're only 
                    # interested in EXISTS ones
                    if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
                        # Got something. Send DONE. This breaks you out of the blocking call
                        @mailbox.idle_done
                    end
                end
                # We're out, which means there are some emails ready for us.
                # Go do a seach for UNSEEN and fetch them.
                process_emails()
            rescue Net::IMAP::Error => imap_err
                # Socket probably timed out
            rescue Exception => gen_err
                puts "Something went terribly wrong: #{e.messsage}"
            end
        end
    end
end
Aldin answered 25/1, 2014 at 0:55 Comment(1)
thanks! finally your example cleared my mind. I just wrote a complete script here: gist.github.com/solyaris/b993283667f15effa579Macdonell
C
1

IMAP IDLE is a feature that mail server implementations can support which allows real-time notifications. [Wikipedia]

The IDLE command may be used with any IMAP4 server implementation that returns "IDLE" as one of the supported capabilities to the CAPABILITY command.

The IDLE command is sent from the client to the server when the client is ready to accept unsolicited mailbox update messages. The server requests a response to the IDLE command using the continuation ("+") response. The IDLE command remains active until the client responds to the continuation, and as long as an IDLE command is active, the server is now free to send untagged EXISTS, EXPUNGE, and other messages at any time.

The IDLE command is terminated by the receipt of a "DONE" continuation from the client; such response satisfies the server's continuation request. [...] The client MUST NOT send a command while the server is waiting for the DONE, since the server will not be able to distinguish a command from a continuation.

[RFC 2177 - IMAP4 IDLE command]

Cacia answered 15/3, 2011 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.