The short answer
- 1Each update has a sequence number (pts) and a count; add and compare to find gaps.
- 2On a gap wait about 0.5 s, then call getDifference (getChannelDifference for channels).
- 3Call getState at login and getDifference after every reconnect.
In this article
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:
| Sequence | What it orders |
|---|---|
pts | Message events in private chats and basic groups; one per account |
Per-channel pts | One for each channel and supergroup |
qts | A second stream, used for things such as secret chats |
seq | The 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 pts | Meaning | What to do |
|---|---|---|
| Equal | Normal | Apply it and store the received number |
| Yours is larger | Already received | Discard it |
| Yours is smaller | A gap: you missed something in between | Fill the gap (below) |
Filling the gap
- 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.
- If the gap is still there, fetch the difference:
updates.getDifferencefor the common sequence,updates.getChannelDifferencefor a channel. - 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
At a glance
- 1Sequences
pts per account, per channel, qts and seq.
- 2Gaps
Smaller stored pts + count means something was missed.
- 3Persist
Save state or messages vanish or repeat on restart.
Updated