gram-js / gramjs

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

Multiple connections #191

Closed danny1ng closed 2 years ago

danny1ng commented 3 years ago

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?

painor commented 3 years ago

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?

danny1ng commented 3 years ago

@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.

painor commented 3 years ago

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

danny1ng commented 3 years ago

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

painor commented 3 years ago

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).

danny1ng commented 2 years ago

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

painor commented 2 years ago

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

danny1ng commented 2 years ago

okay thank