There are two ways to receive updates from TDLib:
1. Event Listeners (client.on)
Attach a callback to the 'update' event. It is highly recommended to also attach a listener to the 'error' event to prevent unhandled promise rejections.
client.on('update', (update) => {
console.log('New update:', update)
})
client.on('error', console.error)
2. Async Iterators (client.iterUpdates)
Introduced in tdl v8.0.0, this allows you to process updates using an async loop. This is often cleaner for sequential processing.
for await (const update of client.iterUpdates()) {
console.log('Received update:', update)
if (update._ === 'updateOption' && update.name === 'my_id') {
break
}
}
Note: The 'close' event is emitted after authorizationStateClosed. Once the client is closed, it can no longer be used to send requests.
// Using event listeners
client.on('update', (update) => {
console.log('New update:', update)
})
// Using async iterators (v8.0.0+)
for await (const update of client.iterUpdates()) {
console.log('Received update:', update)
}