gram-js / gramjs

NodeJS/Browser MTProto API Telegram client library,
MIT License
1.28k stars 179 forks source link

How to handle or listen to disconnection and DC change? #172

Closed Arnique closed 3 years ago

Arnique commented 3 years ago

Sometimes my app will disconnect and switch DC while it's running and my update handlers will be removed.

Is there where to handle or listen to these 2 events?

painor commented 3 years ago

When a file is in another DC the library will try to connect to that DC to download it and then disconnects after it's done. this would not remove any update handlers tho.

Are you sure they are being removed ?

Arnique commented 3 years ago

That is my assumption since updates stop working after I invoke any API method(after app is already connected and running).

This is what is logged when it switches DCs

[2021-10-13T09:55:38.204Z] [INFO] - [Phone migrated to 5]
[2021-10-13T09:55:38.447Z] [INFO] - [Reconnecting to new data center 5]
[2021-10-13T09:55:38.969Z] [INFO] - [Disconnecting from 149.154.167.91:80/TCPFull...]
[2021-10-13T09:55:38.970Z] [INFO] - [Connecting to 91.108.56.114:80/TCPFull...]
[2021-10-13T09:55:38.971Z] [INFO] - [connection closed]
[2021-10-13T09:55:40.328Z] [INFO] - [Connection to 91.108.56.114:80/TCPFull complete!]

I was hoping I could listen to disconnect or even connect events and reset my app then.

painor commented 3 years ago

That is my assumption since updates stop working after I invoke any API method(after app is already connected and running).

This is what is logged when it switches DCs

[2021-10-13T09:55:38.204Z] [INFO] - [Phone migrated to 5]
[2021-10-13T09:55:38.447Z] [INFO] - [Reconnecting to new data center 5]
[2021-10-13T09:55:38.969Z] [INFO] - [Disconnecting from 149.154.167.91:80/TCPFull...]
[2021-10-13T09:55:38.970Z] [INFO] - [Connecting to 91.108.56.114:80/TCPFull...]
[2021-10-13T09:55:38.971Z] [INFO] - [connection closed]
[2021-10-13T09:55:40.328Z] [INFO] - [Connection to 91.108.56.114:80/TCPFull complete!]

I was hoping I could listen to disconnect or even connect events and reset my app then.

This should only happen the first time you login. can you show your code?

Arnique commented 3 years ago

Thanks. It's a lot of code and I'm not permitted to share it so I'll just show the calls. So if or example I want to manually login this is what would happen.

  1. await this.client.connect();

    [INFO] - [Connecting to 149.154.167.91:80/TCPFull...]
    [INFO] - [Connection to 149.154.167.91:80/TCPFull complete!]
  2. await this.client.sendCode(...)

    [INFO] - [Phone migrated to 5]
    [INFO] - [Reconnecting to new data center 5]
    INFO] - [Disconnecting from 149.154.167.91:80/TCPFull...]
    [INFO] - [Connecting to 91.108.56.114:80/TCPFull...]
    [INFO] - [connection closed]
    [INFO] - [Connection to 91.108.56.114:80/TCPFull complete!]
  3. await this.client.invoke(new Api.auth.SignIn({....}))

    [ERROR] - [Unhandled error while receiving data]
    [ERROR] - [Error: Could not find a matching Constructor ID for the TLObject that was supposed to be
        read with ID 571849917. Most likely, a TLObject was trying to be read when
         it should not be read. Remaining bytes: 52]

So basically the first method I call after connection causes DC to switch and subsequent calls fail.

painor commented 3 years ago

Usually using the .start() method is better since it handles all that logic itself.

To start receiving updates you need to call client.getMe() at least once (client;start() does this for you)

Arnique commented 3 years ago

Alright thanks. Went with start()