gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

How to disconnect?

Open felipepimsil opened this issue 3 years ago • 2 comments

There's a way to handle disconnect?

I'm listening to one channel and want to stop the client after i get the message i want, how can i do that?

felipepimsil avatar Jan 21 '22 00:01 felipepimsil

client.disconnect() ?

painor avatar Jan 21 '22 10:01 painor

Either client.disconnect() or client.destroy(). Be aware that they are not the same methods, read documentation for more information.

Jencansee avatar Feb 15 '22 12:02 Jencansee

Hi, I'm trying disconnect and destroy, but the application is still running.

This is my testing snippet:

    const client = new TelegramClient(new StringSession(apiCredentials.sessionHash ?? ''), apiCredentials.apiId, 
    apiCredentials.apiHash, {
      connectionRetries: 5,
    });

    await client.start({
      phoneNumber: apiCredentials.phoneNumber,
      password: undefined,
      phoneCode: async () => codeCallback(),
      onError: (err) => console.log(err),
    });

    const sessionHash = client.session.save();
    console.log("Logged in, the session hash is:", sessionHash);

    apiCredentials.sessionHash = <string><any>sessionHash;
    this.sessionsStorage.setValue(apiCredentials.phoneNumber, apiCredentials);
    //await client.disconnect();
    await client.destroy();

Is there anything more I can do to end TelegramClient loop? Thanks

xluckydegen avatar Jan 18 '23 16:01 xluckydegen

It seems TelegramClient correctly ends after ~1-2 minutes, but long after the client.destroy() is fulfilled.

Is there any way how to wait for the final destruction?

xluckydegen avatar Jan 18 '23 16:01 xluckydegen

There is a hanging promise that runs once a minute the background to ping telegram. the client did disconnect but node won't close the program until that hanging promise is finished. you can process.exit(0) if you need to skip that part.

painor avatar Jan 18 '23 16:01 painor

thanks for the reply. I understand.

Unfortunately, I can't use process.exit(0) because I wanted to use it during the unit-testing of our component.

Is there any chance to get to this hanging promise and wait for it externally?

xluckydegen avatar Jan 18 '23 18:01 xluckydegen

not currently sadly :/ you'd have to manually run it. if you are only using it in testing and won't have long running tasks you can maybe patch the method somehow. it's called _updateLoop

painor avatar Jan 18 '23 19:01 painor