gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

Event listener timeout

Open caesarisme opened this issue 2 years ago • 9 comments
trafficstars

I have noticed that when you subscribe to updates after some time (several hours I guess) it looses connection and new events are not being received. Cron task with restarting connection every 2 hours fixes the problem, but not completely. Any workaround there?

This is my code

const client = new TelegramClient(stringSession, apiId, apiHash, {
    connectionRetries: 5,
  });

await client.connect()

client.addEventHandler(async (event) => {
    await handleSignalMessageEvent(event);
}, new NewMessage({ incoming: true, fromUsers: channelsFromFolder }));

caesarisme avatar Mar 03 '23 16:03 caesarisme

Hello @caesarisme, I haven't noticed the same issue as your code, but I would suggest you call an API before registering the event listener like so

await client.getMe();

// Then addEventHandler

See if that could fix your issue

Ali559 avatar Mar 03 '23 20:03 Ali559

@Ali559 Thank you for response

I already have couple of API requests before addEventHandler. channelsFromFolder in my code is a list of chats from specific folder which I receive from api

caesarisme avatar Mar 04 '23 16:03 caesarisme

Just realized I have this issue as well. Seemed like it's an old issue - probably Telegram servers themselves?

https://github.com/gram-js/gramjs/issues/280

My workaround is to set a cronjob to call getMe() every hour. Gonna see if it works.

ShaunLWM avatar Apr 04 '23 12:04 ShaunLWM

@caesarisme @ShaunLWM Have you been able to find a solution to this issue?

b10c77 avatar Jun 04 '23 18:06 b10c77

@b10c77 Nope. I just do a cron job to restart the service every 6 hours. Working well till now.

ShaunLWM avatar Jun 05 '23 06:06 ShaunLWM

@b10c77 Nope. I just do a cron job to restart the service every 6 hours. Working well till now.

Though it's not clearly documented anywhere, a lot of developers have observed that a high-level method needs to be called to keep the listener alive.

From what I understand, this is on Telegram's API and not a matter of implementation in GramJS or any other client libraries.

I was facing the same problems, but instead of restarting my process again (which leads to missed messages if connect() call takes time) - I have implemented an asynchronous keep-alive mechanism. It looks something like this -

const keepAlive = async () => {
  setInterval(async () => {
    if(!client.connected) {
      await client.connect()
    }

    if(client.checkAuthorization()) {
      await client.getMe() 
      console.log('[Keep Alive]');
    }
  }, 30000);
};

keepAlive();

sahilnarain avatar Jun 15 '23 16:06 sahilnarain

Is it still working? Because I'm facing the exact same problem and the keepAlive function is not solving it

pedroota avatar Aug 29 '23 21:08 pedroota

I am not getting NewMessage event triggered of new channels, seems not related to timeout, I am receiving the new message trigger for old channels but not for the new channels. can someone please help here? It seems the session is alive but there is some internal issue.

devagarwal007 avatar Oct 06 '23 15:10 devagarwal007