Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close

See original GitHub issue

The goal is to get live stream notification, when the profile which I follow starts a live stream. And when some one send a direct message to me, I can get a notification. I am a rookie, so I may have made some low-level mistakes. sorry for that.

I login the account A.

Problem1: I use the example/push.example.ts & realtime.example.ts, but it always shows the ERROR with ‘Premature Close’, and when I use account B to send message to account A, there is no message show in the console.

Problem2: I use readState(ig) and saveState(ig), but the ‘new activity’ in instagram app shows multi login activities. I thought there should be one login activity because after the first login from a never-used device, the second login uses the cookie from the state.json.

 ig:mqtt:realtime Subscribing with Skywalker to ig/u/v1/******, ig/live_notification_subscribe/******** +0ms
  ig:mqtt:realtime Iris Sub to: seqId: ****, snapshot: ****** +0ms
  ig:mqtt:mqttot edge-mqtt.facebook.com: Error: Premature close
  ig:mqtt:mqttot        Stack: Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
  ig:mqtt:mqttot     at new NodeError (node:internal/errors:329:5)
  ig:mqtt:mqttot     at MqttTransformer.onclose (node:internal/streams/end-of-stream:138:38)
  ig:mqtt:mqttot     at MqttTransformer.emit (node:events:381:22)
  ig:mqtt:mqttot     at MqttTransformer.emit (node:domain:470:12)
  ig:mqtt:mqttot     at emitCloseNT (node:internal/streams/destroy:169:10)
  ig:mqtt:mqttot     at processTicksAndRejections (node:internal/process/task_queues:80:21) +0ms
  ig:mqtt:mqttot edge-mqtt.facebook.com: Warning: _a.check is not a function
  ig:mqtt:mqttot        Stack: TypeError: _a.check is not a function
  ig:mqtt:mqttot     at MQTToTClient.setDisconnected (/project/path/node_modules/mqtts/src/mqtt.client.ts:475:55)
  ig:mqtt:mqttot     at /project/path/node_modules/mqtts/src/mqtt.client.ts:175:33
  ig:mqtt:mqttot     at node:internal/util:408:5
  ig:mqtt:mqttot     at finish (node:internal/streams/pipeline:163:7)
  ig:mqtt:mqttot     at node:internal/util:408:5
  ig:mqtt:mqttot     at node:internal/streams/pipeline:74:5
  ig:mqtt:mqttot     at finish (node:internal/streams/pipeline:159:23)
  ig:mqtt:mqttot     at node:internal/util:408:5
  ig:mqtt:mqttot     at node:internal/streams/pipeline:74:5
  ig:mqtt:mqttot     at finish (node:internal/streams/pipeline:159:23)
  ig:mqtt:mqttot     at node:internal/util:408:5
  ig:mqtt:mqttot     at MqttTransformer.<anonymous> (node:internal/streams/pipeline:66:7)
  ig:mqtt:mqttot     at MqttTransformer.<anonymous> (node:internal/util:408:5)
  ig:mqtt:mqttot     at MqttTransformer.onclose (node:internal/streams/end-of-stream:138:25)
  ig:mqtt:mqttot     at MqttTransformer.emit (node:events:381:22)
  ig:mqtt:mqttot     at MqttTransformer.emit (node:domain:470:12)
  ig:mqtt:mqttot     at emitCloseNT (node:internal/streams/destroy:169:10)
  ig:mqtt:mqttot     at processTicksAndRejections (node:internal/process/task_queues:80:21) +0ms
import 'dotenv/config';
import { IgApiClient } from 'instagram-private-api';
import { IgApiClientExt, withRealtime } from 'instagram_mqtt';
import { GraphQLSubscriptions } from 'instagram_mqtt';
import { SkywalkerSubscriptions } from 'instagram_mqtt';
import { promisify } from 'util';
import { writeFile, readFile, exists } from 'fs';

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

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

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

async function loginToInstagram(ig: IgApiClientExt) {
    ig.request.end$.subscribe(() => saveState(ig));
    await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
}


(async () => {
    const ig = withRealtime(new IgApiClient());
    ig.state.generateDevice(process.env.IG_USERNAME);
    ig.state.proxyUrl = process.env.IG_PROXY;

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

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

    await ig.realtime.connect({
        irisData: await ig.feed.directInbox().request(),
        socksOptions: {
            type: 5,
            port: 6153,
            host: '127.0.0.1'
        }
    });

    function logEvent(name: string) {
        return (data: any) => console.log(name, data);
    }

    ig.realtime.on('message', (message) => console.log(message));
    ig.realtime.on('receive', (topic, messages) => {
        console.log('receive', topic, messages);
    });
    ig.realtime.on('error', console.error);
    ig.realtime.on('direct', logEvent('direct'));
})();

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
theshubhampanchalcommented, May 16, 2021

@covein please check updated code. I have changed it. Now it will not login everytime you run your code. It will restore session details from state file and if details are not valid then it will try to login again with credentials and save new session to state file.

0reactions
coveincommented, May 16, 2021

There were some updates to mqtts, not sure if that broke something.

I want to use this to a small project of mine recently. It will be nice if you can update the code. Thanks in advance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node17 compat: Error [ERR_STREAM_PREMATURE_CLOSE ...
I had this issue trying to yard add stripe . Error was YN0001: │ Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close . @bmaier 's yarn set ......
Read more >
Premature close in Node Pipeline stream - Stack Overflow
TL;DR. When using a pipeline you accept to consume the readable stream fully, you don't want anything stopping before the readable ends.
Read more >
ODK Central download all sub with media throws error ...
What is the problem? Be very detailed. When I use the UI to download all submissions for forms with media, I get ERR_STREAM_PREMATURE_CLOSE....
Read more >
I cant create react app - Reddit
0 can't be found in the cache and will be fetched from the remote registry ➤ YN0001: │ Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close at ......
Read more >
Build Error(PREMATURE error) - Kibana - Elastic Discuss
Hi, when I build the kibana source code, I met the error message as below. ... Premature close at Socket.onclose (internal/streams/…
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found