question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NewMessage event callback taking too long to be triggered on big channels

See original GitHub issue

I have an app that uses Telethon to listen to messages from some big channels (2k+ subscribers) and relay them. Since version 0.17 I’ve been noticing some delays in it. So I made the code below to test if the delay was on Telethon and discovered that telethon has been taking over 20s average to run the callback function.

    client = TelegramClient('session', api_id, api_hash, update_workers=1, spawn_read_thread=False).start(phone)

    try:
        client.get_me()
    except RuntimeError:
        print('Couldn\'t login to Telegram. Exiting...')
        sys.exit(0)

    channel= client.get_input_entity('A BIG CHANNEL')
    

    @client.on(events.NewMessage(chats=channel, incoming=True))
    def callback(event):
        print('Message at {} UTC'.format(event.message.date))
        print('Delay was {}s\n'.format((datetime.utcnow() - event.message.date).seconds))

    print('Listening for messages...')
    client.idle()

OUT:

Message at 2018-02-28 03:50:33 UTC
Delay was 52s

Message at 2018-02-28 03:54:23 UTC
Delay was 26s

When listening from a test channel with just me as a subscriber, there is no delay. I’ve been thinking about rolling back my Telethon version, but maybe you guys can help me with it.

UPDATE:

I rolled back the version of the library to 0.16.2 and the issue still persists. I guess it has something to do with the Telegram API itself. Anyway, can anyone help me with this, regardless?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
KrishJodu761commented, Sep 20, 2021

I have an app that uses Telethon to listen to messages from some big channels (2k+ subscribers) and relay them. Since version 0.17 I’ve been noticing some delays in it. So I made the code below to test if the delay was on Telethon and discovered that telethon has been taking over 20s average to run the callback function.

    client = TelegramClient('session', api_id, api_hash, update_workers=1, spawn_read_thread=False).start(phone)

    try:
        client.get_me()
    except RuntimeError:
        print('Couldn\'t login to Telegram. Exiting...')
        sys.exit(0)

    channel= client.get_input_entity('A BIG CHANNEL')
    

    @client.on(events.NewMessage(chats=channel, incoming=True))
    def callback(event):
        print('Message at {} UTC'.format(event.message.date))
        print('Delay was {}s\n'.format((datetime.utcnow() - event.message.date).seconds))

    print('Listening for messages...')
    client.idle()

OUT:

Message at 2018-02-28 03:50:33 UTC
Delay was 52s

Message at 2018-02-28 03:54:23 UTC
Delay was 26s

When listening from a test channel with just me as a subscriber, there is no delay. I’ve been thinking about rolling back my Telethon version, but maybe you guys can help me with it.

UPDATE:

I rolled back the version of the library to 0.16.2 and the issue still persists. I guess it has something to do with the Telegram API itself. Anyway, can anyone help me with this, regardless?

I am also facing same issue but less delay like 1 or 2 or 3 or 5 seconds, But i need to get 0 sec delay means it should arrive when it sent, Is that delay problem solved? please provide some information or code regarding to that, so that will very helpful for those who are facing this issue.

1reaction
Lonamicommented, Mar 10, 2020

There’s a timeout somewhere returned by the API which indicates how often one should call getDifference. I guess you can manually “watch” channels you’re interested in with it, but I’ve never tried this because I’ve never seen any delays, so while I think it works I’m not sure. Do not ask how to use it here, we have a group for that and StackOverflow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

client.on(events.NewMessage) no longer captures all events
I have a problem: until a few months ago "@client.on(events.NewMessage)" used to capture all messages coming to me from all groups and channels, ......
Read more >
Update Events — Telethon 1.26.0 documentation
Represents the event of a new message. This event can be treated to all effects as a Message , so please refer to...
Read more >
Telethon Documentation - Read the Docs
NewMessage event, the callback function you're about to define will be ... The code of your application starts getting big, so you decide...
Read more >
Working with Updates — Telethon 1.7.7 documentation
This Python decorator will attach itself to the my_event_handler definition, and basically means that on a NewMessage event, the callback function you're about ......
Read more >
Asynchronous messaging options - Azure Architecture Center
Interested consumer(s), can subscribe, listen for events, and take actions ... When a new message is available, the consumer's callback is invoked, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found