chirag04/mail-listener2

imap disconnected every few hours

Opened this issue ยท 13 comments

Is there a way to prolong the life of the mail listener connection without having to refresh the code every few hours? I notice that the mail listener disconnects after a while of no traffic.

Thanks

@prashb94 same things. Did you get issue fixed?

Hey,

I just start the connection again if the server disconnects. A temporary fix for now.

On Jul 18, 2016 06:42, dpcolaberry notifications@github.com wrote:

@prashb94https://github.com/prashb94 same things. Did you get issue fixed?

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com//issues/54#issuecomment-233208549, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AKUq3C1j3RdgHnblPCkTF-HI-2LReh6Zks5qWq_NgaJpZM4JBEHR.

+1

Any news?

@prashb94 as simple as this?

mailListener.on('server:disconnected', () => { console.log('imapDisconnected'); mailListener.stop(); mailListener.start(); });

I've also noticed that sometimes due to the Idle event firing it can miss emails that have not previously been read. Weirdly if you restart the client it STILL ignores those received emails. You have to mark them as seen and then back to unread for them to parse.

@Hardware-Hacks your solution did not work for me! I've tried two ways:
1

        function startListener() {
		if (mailListener) {
			mailListener.stop();
		}
		mailListener = new MailListener(mailListenerOpts);
		mailListener.start();
	}

	startListener();

	mailListener.on('server:connected', () => {
		console.info('IMAP connected');
	});

	mailListener.on('server:disconnected', () => {
		startListener();
	});

	mailListener.on('error', errorHandler);
	mailListener.on('mail', mailHandler);
```

2
```
        mailListener = new MailListener(mailListenerOpts)
	mailListener.start();

	function restartListener() {
		mailListener.stop();
		mailListener.start();
	}

	mailListener.on('server:connected', () => {
		console.info('IMAP connected');
	});

	mailListener.on('server:disconnected', () => {
		restartListener();
	});

	mailListener.on('error', errorHandler);
	mailListener.on('mail', mailHandler);
```

What I do now (although does not sound good) I use forever CLI to ensure that app always run, and then:

	mailListener.on('server:disconnected', () => {
		console.warn('IMAP disconnected');
		throw new Error('Restart app due to IMAP disconnection');
            
                // or process.exit(1); 
               // or process. exitCode =1;
	});

@amirhouieh fully agree there is something fundamentally wrong here and I can't fathom it. I even produced mail-listener-fixed which incorporated the best bits from all the forks in this area. I think there are at least 2 things going wrong, 1 of which is even hitting a timeout, if you use oauth you won't hit the same issue. 2 the IDLE command doesnt seem to receive responses after a while, or it is breaking down on the client side causing the server to disconnect the connection.

I've actually ditched all of this now. For GMail specifically I switched over to context.io and use their npm library to retrieve emails via live sync. It works really good and offers the same functionality.

add on node-modules/mail-listener2/index.js
this:
MailListener.prototype.start = function() {
this.imap.on('ready', imapReady.bind(this));
this.imap.on('close', imapClose.bind(this));
this.imap.connect();
};
MailListener.prototype.restart = function() {
console.log('detaching existing listener');
this.imap.removeAllListeners('mail');
this.imap.removeAllListeners('update');

console.log('calling imap connect');
this.imap.connect();
};

and then use it