gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

Multiple connections

Open danny1ng opened this issue 4 years ago • 8 comments

I have code like this.

const { StringSession } = require('telegram/sessions')
const input = require('input') // npm i input

const apiId = 111111
const apiHash = 'fcf............................'
const stringSession = new StringSession(''); // fill this later with the value from session.save()
(async () => {
    console.log('Loading interactive example...')
    const client = new TelegramClient(stringSession, apiId, apiHash, { connectionRetries: 5 })
    await client.start({
        phoneNumber: async () => await input.text('number ?'),
        password: async () => await input.text('password?'),
        phoneCode: async () => await input.text('Code ?'),
        onError: (err) => console.log(err),
    });
    console.log('You should now be connected.')
    console.log(client.session.save()) // Save this string to avoid logging in again
  async function eventPrint(event: NewMessageEvent) {
  console.log(event.message);
  };
  client.addEventHandler(eventPrint, new NewMessage({ chats: [chatId] }));
})()

When i run this code on express server in different por only work last telegram. That is, the last running one always works, the previous ones do not work. It feels like the new connection is killing the old one. How to solve the problem with multi-connection?

danny1ng avatar Nov 11 '21 14:11 danny1ng

I'm sorry but I don't understand what you mean.

the connection is tied to the client object. if you create a new TelegramClient you are creating a new connection.

if you want to have multiple connections maybe put the clients inside an array?

painor avatar Nov 11 '21 14:11 painor

@painor i have express app which find message in telegram channel and i want run 1 app for one channel and 2 app on another chanel. i need run telegram in different terminals . Array not suits me.

danny1ng avatar Nov 11 '21 14:11 danny1ng

I don't see the issue then. if you have multiple running clients on different terminals it shouldn't be a problem

painor avatar Nov 11 '21 21:11 painor

I don't see the issue then. if you have multiple running clients on different terminals it shouldn't be a problem

i recorded video with issues https://monosnap.com/file/d1wm4zvDI0Zltr0WXO7rnWBB3xG0n9

danny1ng avatar Nov 14 '21 14:11 danny1ng

you can't run the same session multiple times. Telegram will only send updates to the latest running client.

if you want to receive updates you'll need a new session for each client (even if it's the same account).

painor avatar Nov 14 '21 18:11 painor

can I save the connection to the database and work with the client without connecting? I see that the client is different before and after the connection. image

in this example https://github.com/abdhass/telegram-channel-scraper/blob/master/utils/init.js in another library I see a storage where your connection is saved, and you can work with 1 session and make multiple connections

danny1ng avatar Nov 16 '21 20:11 danny1ng

GramJS returns a SessionString that you can save in your DB if you want.

you can use the same session string in the same IP to send messages

You would need a different session string if you want to receive messages/updates. it's a telegram limitation

painor avatar Nov 17 '21 13:11 painor

okay thank

danny1ng avatar Nov 19 '21 19:11 danny1ng