Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listener active even after stopped ? #5

Open
gkrcode opened this issue Apr 16, 2012 · 6 comments
Open

Listener active even after stopped ? #5

gkrcode opened this issue Apr 16, 2012 · 6 comments

Comments

@gkrcode
Copy link

gkrcode commented Apr 16, 2012

Hi,

require 'em-imap'

EM.run do

    client = EM::IMAP.new("imap.gmail.com",993,true)

    client.connect.bind! do
        client.login("[email protected]","some_gmail_password")
    end.bind! do
        client.select("INBOX")
    end.bind! do
        idler = client.idle

        idler.listen do |response|
            if (response == "stop" rescue nil)
                puts "Stop"
                idler.stop
                idler.callback do
                    puts "Stop: Callback"
                end             
            elsif (response == "start" rescue nil)
                puts "Still Listening"          
            elsif (response.name == "EXISTS" rescue nil)
                puts "Ooh, new emails!"
                idler.stop
                idler.callback do
                    # ... process new emails
                end
            end
        end.errback do |e|
            puts "Idler recieved an error: #{e}"
        end

        EM.add_periodic_timer(2) do
            idler.receive_event "start"
        end 

        EM.add_timer(6) do
            idler.receive_event "stop"
        end 

    end
end

Output:

Still Listening
Still Listening
Stop
Still Listening
Stop: Callback
Still Listening
Still Listening
Still Listening
Still Listening
...
...
...

Listener is active even after stopped, is this normal behavior ?

Thanks

@gkrcode
Copy link
Author

gkrcode commented May 9, 2012

@ConradIrwin any help ?

@ConradIrwin
Copy link
Owner

Hi @gkrcode,

This was actually intentional. If you're using the listener API you should add a .stopback{} that cleans up your event senders.

The IDLE command relies on this behaviour because if it stopped receiving events after you call .stop, it would never internally hear the "command OK" response in order to be able to .succeed.

That said, it's pretty confusing... Maybe I should rethink how it is structured.

@gkrcode
Copy link
Author

gkrcode commented May 14, 2012

Thanks @ConradIrwin,

If you're using the listener API you should add a .stopback{} that cleans up your event senders.

I don't get it please share a code sample.

That said, it's pretty confusing... Maybe I should rethink how it is structured.

+1 Looking forward :)

@nazarhussain
Copy link

Hi @gkrcode

Have you found the solution for this issue? And one more question to you how can we actually get emails within idle.callback block? It is giving <struct Net::IMAP::TaggedResponse tag="RUBY0003", name="OK", data=#<struct Net::IMAP::ResponseText code=nil, text="IDLE terminated (Success)">, raw_data="RUBY0003 OK IDLE terminated (Success)\r\n"> but I don't know how can I get actual email from this response?

@gkrcode
Copy link
Author

gkrcode commented Nov 19, 2012

Hi @nazarhussain , I am no longer using em-imap :(

@nazarhussain
Copy link

@gkrcode Do you find any alternate for it? I want to achieve this task, if you can guide me in it. #7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants