Nerixyz / instagram_mqtt

Realtime and Push Notification (FBNS) support for the instagram-private-api
MIT License
252 stars 50 forks source link

Error: MQTToTClient got disconnected #18

Open amirpdlll opened 4 years ago

amirpdlll commented 4 years ago

The library is working fine and I appreciate you , but when I connect using command line windows with command

ts-node example.ts

first every thing works fine and all events are printing , but after about one minute I face error

Error: MQTToTClient got disconnected

Please see this image is taken from command line https://prnt.sc/qxvzji

thanks

Nerixyz commented 4 years ago

I'll look at it later. But for now, could you turn on debugging.

amirpdlll commented 4 years ago

I'll look at it later. But for now, could you turn on debugging.

I have created .env file in root and I have added this line DEBUG=ig:*

I checked it again I face same error and there is extra information

ig:mqtt:mqttot edge-mqtt.facebook.com:443: Disconnected. +0ms

https://prnt.sc/qy09ur

what is wrong ? how can I fix it ?

thanks

amirpdlll commented 4 years ago

I'll look at it later. But for now, could you turn on debugging.

In command line I see these commands

Device off ig:mqtt:realtime:direct Updated foreground state: {"inForegroundApp":false,"inForegroundDevice":false,"keepAliveTimeout":900} +0ms In App ig:mqtt:realtime:direct Updated foreground state: {"inForegroundApp":true,"inForegroundDevice":true,"keepAliveTimeout":60} +2s

and about one minute after it , it gets disconnected.

if this is the problem 'keepAliveTimeout' how can I increase it ?

amirpdlll commented 4 years ago

I'll look at it later. But for now, could you turn on debugging.

In command line I see these commands

Device off ig:mqtt:realtime:direct Updated foreground state: {"inForegroundApp":false,"inForegroundDevice":false,"keepAliveTimeout":900} +0ms In App ig:mqtt:realtime:direct Updated foreground state: {"inForegroundApp":true,"inForegroundDevice":true,"keepAliveTimeout":60} +2s

and about one minute after it , it gets disconnected.

if this is the problem 'keepAliveTimeout' how can I increase it ?

I have increased it but nothing changed !

Nerixyz commented 4 years ago

I'm experiencing the same problem. For now, you can try an older version (0.1.x). Edit: seems like the old version still works fine

amirpdlll commented 4 years ago

I'm experiencing the same problem. For now, you can try an older version (0.1.x). Edit: seems like the old version still works fine

Thanks you ,

I have used https://github.com/Nerixyz/instagram_mqtt#below-015-old

and now my code is


import {FbnsClient, RealtimeClient} from './src';
import {IgApiClient} from 'instagram-private-api';

(async () => {

    const ig = new IgApiClient();

// norm
```al login
    ig.state.generateDevice('USERNAME');
    await ig.account.login('USERNAME', 'PASSWORD');
// now `ig` is a client with a valid session

// Initialize the client
// This includes logging in (if you are below 0.1.6)
// ig.account.login ...
    const fbns = new FbnsClient(ig);
    await fbns.connect();

    const realtime = new RealtimeClient(ig);
    await realtime.connect();

    console.log('we are here');
// whenever something gets sent and has no event, this is called
    realtime.on('receive', (topic, messages) => {
        console.log('receive', topic, messages);
    });

    realtime.on('direct', (messages) => {
        console.log('receive2');
    });
// this is called with a wrapper use {message} to only get the message from the wrapper
    realtime.on('message', (messages) => {
        console.log('receive2');
    });

})();

but I think I made mistake because I'm newbie , actually I don't know how to grab new messages and I didn't find example , when I launch this I just got this :
https://prnt.sc/qy1uss

Can you please provide example of using old version ?

thank you
Nerixyz commented 4 years ago

You can use npm i instagram_mqtt@0.1.17 and this tree.

amirpdlll commented 4 years ago

In a new folder I have installed this version and using example in the tree that you mentioned I created example.ts

this is the code


> import { IgApiClientRealtime, withRealtime } from './src';
> import { GraphQLSubscriptions } from './src/realtime/subscriptions';
> import { IgApiClient } from 'instagram-private-api';
> import { SkywalkerSubscriptions } from './src/realtime/subscriptions';
> 
> (async () => {
>     // this extends the IgApiClient with realtime features
>     const ig: IgApiClientRealtime = withRealtime(new IgApiClient());
>     // normal login
>     ig.state.generateDevice('USERNAME');
>     await ig.account.login('USERNAME', 'PASSWORD');
>     // now `ig` is a client with a valid session
> 
>     // an example on how to subscribe to live comments
>     const subToLiveComments = (broadcastId) =>
>         // you can add other GraphQL subs using .subscribe
>         ig.realtime.graphQlSubscribe(GraphQLSubscriptions.getLiveRealtimeCommentsSubscription(broadcastId));
> 
>     // whenever something gets sent and has no event, this is called
>     ig.realtime.on('receive', (topic, messages) => {
>         console.log('receive', topic, messages);
>     });
>     ig.realtime.on('direct', logEvent('direct'));
>     // this is called with a wrapper use {message} to only get the message from the wrapper
>     ig.realtime.on('message', logEvent('messageWrapper'));
>     // whenever something gets sent to /ig_realtime_sub and has no event, this is called
>     ig.realtime.on('realtimeSub', logEvent('realtimeSub'));
>     // whenever the client has a fatal error
>     ig.realtime.on('error', console.error);
>     ig.realtime.on('close', () => console.error('RealtimeClient closed'));
>     // connect
>     // this will resolve once all initial subscriptions have been sent
>     await ig.realtime.connect({
>         // optional
>         graphQlSubs: [
>             // these are some subscriptions
>             GraphQLSubscriptions.getAppPresenceSubscription(),
>             GraphQLSubscriptions.getClientConfigUpdateSubscription(),
>             GraphQLSubscriptions.getZeroProvisionSubscription(ig.state.phoneId),
>             GraphQLSubscriptions.getDirectStatusSubscription(),
>             GraphQLSubscriptions.getDirectTypingSubscription(ig.state.cookieUserId),
>             GraphQLSubscriptions.getAsyncAdSubscription(ig.state.cookieUserId),
>         ],
>         // optional
>         skywalkerSubs: [
>             SkywalkerSubscriptions.directSub(ig.state.cookieUserId),
>             SkywalkerSubscriptions.liveSub(ig.state.cookieUserId)
>         ],
>         // optional
>         // this enables you to get direct messages
>         irisData: await ig.feed.directInbox().request(),
>         // optional
>         // in here you can change connect options
>         // available are all properties defined in MQTToTConnectionClientInfo
>         connectOverrides: {
>         },
>     });
> 
>     // simulate turning the device off after 2s and turning it back on after another 2s
>     setTimeout(() => {
>         console.log('Device off');
>         // from now on, you won't receive any realtime-data as you "aren't in the app"
>         // the keepAliveTimeout is somehow a 'constant' by instagram
>         ig.realtime.direct.sendForegroundState({inForegroundApp: false, inForegroundDevice: false, keepAliveTimeout: 900});
>     }, 2000);
>     setTimeout(() => {
>         console.log('In App');
>         ig.realtime.direct.sendForegroundState({inForegroundApp: true, inForegroundDevice: true, keepAliveTimeout: 60});
>     }, 4000);
> })();
> 
> /**
>  * A wrapper function to log to the console
>  * @param name
>  * @returns {(data) => void}
>  */
> function logEvent(name) {
>     return data => console.log(name, data);
> }

when I launch it using ts-node example.ts I face this error

https://prnt.sc/qy2hgb

I don't know may be I'm making mistake !

Nerixyz commented 4 years ago

After ig.state.generateDevice('USERNAME'); add await ig.qe.syncLoginExperiments()

Nerixyz commented 4 years ago

Version 0.2.1 might fix your issue.

amirpdlll commented 4 years ago

Version 0.2.1 might fix your issue.

thank you so much , for now I'm testing 1.1 , this is good but on windows some time it break and get back to command line This is my command line : https://prnt.sc/qy42wk

I'm going to test v 2.1 thank you

amirpdlll commented 4 years ago

Version 0.2.1 might fix your issue.

Thank you , I have tested 2.1 and seems it is working properly , thank to you and your support :)

amirpdlll commented 4 years ago

Version 0.2.1 might fix your issue.

How can I get user_id and send response? there was not send direct message and response in example !

amirpdlll commented 4 years ago

Version 0.2.1 might fix your issue. thank you so much ,

Version 0.2.1 might fix your issue.

How can I get user_id and send response? there was not send direct message and response in example !

Found https://github.com/Nerixyz/instagram_mqtt/issues/12 thank you

amirpdlll commented 4 years ago

unfortunately , 2.1 is like 2.0 and it got disconnected after about 1 or 2 minutes , is there any way to solve it ? thanks

amirpdlll commented 4 years ago

when I use 1.17 in command line , without raising any error it exit operating https://prnt.sc/qz957p

beside when I'm using fbns (push in example) I get igSentryBlockError

why is it happening and how can I fix it ? thanks

Nerixyz commented 4 years ago

Disabling getClientConfigUpdateSubscription and getZeroProvisionSubscription works. I don't know why that's the case.

Androz2091 commented 4 years ago

I'm getting the same error (using 0.2.1).

Androz2091 commented 4 years ago

I also receive all events twice... 🤔 For example:

ig.fbns.push$.subscribe((data) => {
    if(data.collapseKey === "direct_v2_message"){
        console.log('message received', data.message)
    }
});

For one message sent, it logs two "message received" (with the same content).

amirpdlll commented 4 years ago

Disabling getClientConfigUpdateSubscription and getZeroProvisionSubscription works. I don't know why that's the case.

after disabling it's working thanks

amirpdlll commented 4 years ago

I'm getting the same error (using 0.2.1).

I'm using V 1.17 and real time , for now it's working good , try it

Androz2091 commented 4 years ago

Ah, I'm using the FBNS client.

amirpdlll commented 4 years ago

Ah, I'm using the FBNS client.

Why you don't use real time ?

Androz2091 commented 4 years ago

Is it better? I need to trigger a function when a message is received, and as I'm making a chatbot I need to keep the bot logged forever.

Nerixyz commented 4 years ago

Is it better? I need to trigger a function when a message is received, and as I'm making a chatbot I need to keep the bot logged forever.

Realtime would be in this case more reliable. The notifications are managed by facebook and if you're receiving many messages at once, it will omit some notifications. Realtime on the other hand will most likely still send the messages. And in addition you can get the entire message/image etc. and you don't have to fetch this on your own.

Androz2091 commented 4 years ago

I tested realtime and that's amazing! 😮 Thank you so much @Nerixyz!

Eonus21 commented 4 years ago

What is the right way to keep connection for long time? So, now I am using all tips above. But where can I catch MQTT Client disconnect rightly? 'error' event or 'disconnect'? And then, should I call realtime.connect as a first times or there is a method to reconnecting? I am using instagram-mqtt@0.2.13

Nerixyz commented 4 years ago

But where can I catch MQTT Client disconnect rightly?

Currently, an error is emitted if the connection is closed unexpectedly. If you call.disconnect(), the disconnect event is fired.

Eonus21 commented 4 years ago

So, I need write something like

realtime.on('error', err => {
  if (disconnect_error) {
      //realtime.connect as first times
  }
})

yes? And will my handlers working with new connection (as error handler)?

Nerixyz commented 4 years ago

So, I need write something like

I just updated the package (0.2.14). Now, the client reconnects (and subscribes) automatically so you don't have to do anything.

Eonus21 commented 4 years ago

Vielen Dank, ich hoffe, dass ich keine Probleme haben werde.

jafferkazmi572 commented 4 years ago

So, I need write something like

I just updated the package (0.2.14). Now, the client reconnects (and subscribes) automatically so you don't have to do anything.

Hey @Nerixyz great work, but i am using 0.2.16 with both Realtime and Fbns . my connection is still disconnecting after some time and not reconnecting what should i do. thanks

Androz2091 commented 4 years ago

Same for me! It closes the node process... and never reconnects (as the node process is closed).

ClientDisconnectedError: MQTToTClient got disconnected.
at SafeSubscriber._next (/home/container/node_modules/instagram_mqtt/dist/realtime/realtime.client.js:152:30)
at SafeSubscriber.__tryOrUnsub (/home/container/node_modules/rxjs/internal/Subscriber.js:205:16)
at SafeSubscriber.next (/home/container/node_modules/rxjs/internal/Subscriber.js:143:22)
at Subscriber._next (/home/container/node_modules/rxjs/internal/Subscriber.js:89:26)
at Subscriber.next (/home/container/node_modules/rxjs/internal/Subscriber.js:66:18)
at Subject.next (/home/container/node_modules/rxjs/internal/Subject.js:60:25)
at MQTToTClient.setDisconnected (/home/container/node_modules/mqtts/dist/mqtt.client.js:329:30)
at Object.disconnect (/home/container/node_modules/mqtts/dist/mqtt.client.js:97:26)
at TLSSocket.<anonymous> (/home/container/node_modules/mqtts/dist/transport/tls.transport.js:20:52)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Nerixyz commented 4 years ago

There's a "new" alpha version which aims to fix some of these issues by not usign rxjs and instead relying on regular events and streams. The events are fully typed so they will work fine with TypeScript. There should also be autocomplete support (at least in WebStorm) for the events. You can get the alpha version here (it's on the alpha branch).

Androz2091 commented 4 years ago

I'm testing it, but I made my own parser for iris messages. Is there still a way to receive all iris messages as they are, without being parsed?

Nerixyz commented 4 years ago

Is there still a way to receive all iris messages as they are, without being parsed?

There are Mixins now, you may want to use them (like the other ones do). But what's the problem with the parsing?

Androz2091 commented 4 years ago

There are Mixins now, you may want to use them (like the other ones do).

All right 👍

But what's the problem with the parsing?

I made my own parser, so it means I have to rewrite it?

Nerixyz commented 4 years ago

I made my own parser, so it means I have to rewrite it?

If you still have a need for it, yeah. But the only difference was the handling of direct_v2/inbox which will be sent on threadUpdate.

kevinstrange7 commented 4 years ago

Screenshot_20201001-112027024 (1)

How about this error? @Nerixyz ? i'm using alpha latest version. how to get connect again

Nerixyz commented 4 years ago

how to get connect again

If you're using the default settings, then the client should reconnect automatically. The error is emitted either way. If the client doesn't reconnect, please paate the debug log here (DEBUG=*).

kevinstrange7 commented 4 years ago

how to get connect again

If you're using the default settings, then the client should reconnect automatically. The error is emitted either way. If the client doesn't reconnect, please paate the debug log here (DEBUG=*).

Hi @Nerixyz thanks for answer, btw can you give me a fbns example for alpha version?

kevinstrange7 commented 4 years ago

Hi @Nerixyz Thanks!

i just tested and working

before i using await ig.fbns.connect( { autoReconnect:true } ) fix by await ig.fbns.connect()

jafferkazmi572 commented 3 years ago

There's a "new" alpha version which aims to fix some of these issues by not usign rxjs and instead relying on regular events and streams. The events are fully typed so they will work fine with TypeScript. There should also be autocomplete support (at least in WebStorm) for the events. You can get the alpha version here (it's on the alpha branch).

Hey @Nerixyz @Androz2091 , i am not able to use the alpha and got following error after installing Screenshot from 2020-10-16 14-53-12

i am using

WebStorm IDE Node 10.13.0 OS Linux Fedora 30 Package Manager YARN

i have also tried NPM but when i install it with npm it gives error instagram-private-api not found at run time but it actually installed

Nerixyz commented 3 years ago

@jafferkazmi572 You have to update your node to at least v.12.

jafferkazmi572 commented 3 years ago

@jafferkazmi572 You have to update your node to at least v.12.

Ok Thanks