gramjs
gramjs copied to clipboard
How to disconnect?
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?
client.disconnect() ?
Either client.disconnect() or client.destroy(). Be aware that they are not the same methods, read documentation for more information.
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
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?
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.
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?
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