gramjs
gramjs copied to clipboard
Event listener timeout
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 }));
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 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
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.
@caesarisme @ShaunLWM Have you been able to find a solution to this issue?
@b10c77 Nope. I just do a cron job to restart the service every 6 hours. Working well till now.
@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();
Is it still working? Because I'm facing the exact same problem and the keepAlive function is not solving it
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.