Nerixyz / instagram_mqtt

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

ClientDisconnectedError: MQTToTClient got disconnected. #83

Closed deri387 closed 3 years ago

deri387 commented 3 years ago

Please help me, i got the error like this

ClientDisconnectedError: MQTToTClient got disconnected.

Screen Shot 2021-07-09 at 15 56 09
Nerixyz commented 3 years ago

What's your code and does the client reconnect?

seanconrad1 commented 3 years ago

I'm running into the same issue on startup. The client does not reconnect. See code below

import {
  withRealtime,
  GraphQLSubscriptions,
  SkywalkerSubscriptions,
} from "instagram_mqtt";
import { IgApiClient } from "instagram-private-api";
import { promisify } from "util";
import { writeFile, readFile, exists } from "fs";

const writeFileAsync = promisify(writeFile);
const readFileAsync = promisify(readFile);
const existsAsync = promisify(exists);

(async () => {
  const ig = withRealtime(new IgApiClient() /* you may pass mixins in here */);
  ig.state.generateDevice("${myUsername}");

  // this will set the auth and the cookies for instagram
  await readState(ig);

  // this logs the client in
  await loginToInstagram(ig);

  ig.realtime.on("receive", (topic, messages) =>
    console.log("receive", topic, messages)
  );

  // this is called with a wrapper use {message} to only get the "actual" message from the wrapper
  ig.realtime.on("message", logEvent("messageWrapper"));

  // a thread is updated, e.g. admins/members added/removed
  ig.realtime.on("threadUpdate", logEvent("threadUpdateWrapper"));

  // other direct messages - no messages
  ig.realtime.on("direct", logEvent("direct"));

  // 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.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: {},

    // optional
    // use this proxy
    socksOptions: {
      type: 5,
      port: 12345,
      host: "...",
    },
  });

  // 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);
})();

async function saveState(ig) {
  return writeFileAsync("state.json", await ig.exportState(), {
    encoding: "utf8",
  });
}

async function readState(ig) {
  if (!(await existsAsync("state.json"))) return;
  await ig.importState(await readFileAsync("state.json", { encoding: "utf8" }));
}

async function loginToInstagram(ig) {
  ig.request.end$.subscribe(() => saveState(ig));
  await ig.account.login("${myUsername}", "${myPassword}");
}

/**
 * A wrapper function to log to the console
 * @param name
 * @returns {(data) => void}
 */
function logEvent(name) {
  return (data) => console.log(name, data);
}
Nerixyz commented 3 years ago

I'm running into the same issue on startup. The client does not reconnect. See code below

Try to remove the socksOptions.

seanconrad1 commented 3 years ago

Thanks @Nerixyz, I commented out that object and I'm getting this error now

TypeError: stream.on is not a function
    at destroyer (internal/streams/pipeline.js:27:10)
    at internal/streams/pipeline.js:79:12
    at Array.map (<anonymous>)
    at Function.pipeline (internal/streams/pipeline.js:76:28)
    at MQTToTClient.createPipeline (node_modules/mqtts/dist/mqtt.client.js:110:34)
    at MQTToTClient._connect (node_modules/mqtts/dist/mqtt.client.js:95:14)
    at async MQTToTClient.connect (node_modules/mqtts/dist/mqtt.client.js:100:13)
Nerixyz commented 3 years ago

I commented out that object and I'm getting this error now

See #81. tl;dr your node version is probably too old.

seanconrad1 commented 3 years ago

@Nerixyz Thank you that fixed it!