Skip to content
TGWAY
Channels & Bots STEP 83 / 99

Telegram API Updates: Catching Up on Missed Messages

Telegram API updates carry sequence numbers. Compare pts and pts_count to spot gaps, wait briefly, then fill them with getDifference or getChannelDifference

  • Applies toDevelopers
  • LevelL4
  • Reading time4 min
  • Verified

The short answer

  1. 1Each update has a sequence number (pts) and a count; add and compare to find gaps.
  2. 2On a gap wait about 0.5 s, then call getDifference (getChannelDifference for channels).
  3. 3Call getState at login and getDifference after every reconnect.
In this article
  1. Updates arrive with sequence numbers
  2. Spotting a gap
  3. Filling the gap
  4. At startup and after reconnecting
  5. If you use a library
  6. FAQ
  7. At a glance

Updates arrive with sequence numbers

A user-account app learns about new messages, edits and deletions through updates the server pushes to it. Updates can go missing or arrive out of order when a connection drops, so Telegram gives each one a sequence number that lets your app notice a gap. There are separate sequences:

SequenceWhat it orders
ptsMessage events in private chats and basic groups; one per account
Per-channel ptsOne for each channel and supergroup
qtsA second stream, used for things such as secret chats
seqThe order of update containers themselves

Spotting a gap

An update carries the sequence number after this event (pts) and how many places the event took up (pts_count). Add the count to your stored number and compare the result with the one you received.

Your pts + pts_count vs received ptsMeaningWhat to do
EqualNormalApply it and store the received number
Yours is largerAlready receivedDiscard it
Yours is smallerA gap: you missed something in betweenFill the gap (below)

Filling the gap

  1. The order may only be briefly mixed up, so wait about half a second to see whether the missing update arrives. The official documentation recommends this.
  2. If the gap is still there, fetch the difference: updates.getDifference for the common sequence, updates.getChannelDifference for a channel.
  3. A large difference comes in several parts. Call again with the new state from each reply until you have everything.

If the server sends updatesTooLong, it means there is too much to push; go straight to fetching the difference.

At startup and after reconnecting

Right after login, call updates.getState and store the current numbers. Updates are pushed to the connection that was active most recently, so whenever your app wakes up or reconnects, call getDifference first to catch up on what happened meanwhile.

If you use a library

TDLib does all of this sequence tracking and gap filling for you and hands your app clean results; see building TDLib. You only need the rules above if you implement MTProto yourself. The Bot API's update_id is a different, much simpler number; see webhooks vs long polling.

FAQ

What is pts in the Telegram API?

A sequence number on updates. Comparing your stored pts plus pts_count with the received pts tells you whether you missed something.

How do I get missed Telegram updates?

Call updates.getDifference, or updates.getChannelDifference for a channel, and repeat with the new state until done.

Do I need this with TDLib?

No. TDLib tracks sequences and fills gaps for you.

Sources

Summary

At a glance

Telegram API Updates: Catching Up on Missed Messages — at a glance illustration
  1. 1Sequences

    pts per account, per channel, qts and seq.

  2. 2Gaps

    Smaller stored pts + count means something was missed.

  3. 3Persist

    Save state or messages vanish or repeat on restart.

Share this guide

Send it to a friend who has the same question.

Telegram WhatsApp
More

Open on your phonePoint your phone camera here to open this guide.

Updated

Related articles

View all ›