how to catch internet disconnection event while downloading a big file in node js
I need to download a big file from remote server ( for example: from amazon )
But I have very bad internet connection, so it can disconnect for some seconds and automatically reconnect, but in this case downloading freezes and I can't catch this event programatically.
I use code very similar to this:
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err)) // I suppose that here I will catch all possible errors like internet disconnect, but seems like no
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So for example if I need to download 500MB file, and I have already downloaded for example 100MB and suddenly I lost internet connection so the file will not be downloaded anymore and no errors will raise.
please help
node.js http request
add a comment |
I need to download a big file from remote server ( for example: from amazon )
But I have very bad internet connection, so it can disconnect for some seconds and automatically reconnect, but in this case downloading freezes and I can't catch this event programatically.
I use code very similar to this:
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err)) // I suppose that here I will catch all possible errors like internet disconnect, but seems like no
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So for example if I need to download 500MB file, and I have already downloaded for example 100MB and suddenly I lost internet connection so the file will not be downloaded anymore and no errors will raise.
please help
node.js http request
Does the download resume when the connection comes back online?
– Gordon
Nov 15 '18 at 18:56
See this answer for how to detect drops.
– Gordon
Nov 15 '18 at 19:03
Thank you very much, I will try this approach. Actually downloading does not resume in my case
– Dengue
Nov 16 '18 at 6:17
add a comment |
I need to download a big file from remote server ( for example: from amazon )
But I have very bad internet connection, so it can disconnect for some seconds and automatically reconnect, but in this case downloading freezes and I can't catch this event programatically.
I use code very similar to this:
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err)) // I suppose that here I will catch all possible errors like internet disconnect, but seems like no
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So for example if I need to download 500MB file, and I have already downloaded for example 100MB and suddenly I lost internet connection so the file will not be downloaded anymore and no errors will raise.
please help
node.js http request
I need to download a big file from remote server ( for example: from amazon )
But I have very bad internet connection, so it can disconnect for some seconds and automatically reconnect, but in this case downloading freezes and I can't catch this event programatically.
I use code very similar to this:
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err)) // I suppose that here I will catch all possible errors like internet disconnect, but seems like no
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So for example if I need to download 500MB file, and I have already downloaded for example 100MB and suddenly I lost internet connection so the file will not be downloaded anymore and no errors will raise.
please help
node.js http request
node.js http request
edited Nov 15 '18 at 18:42
Dengue
asked Nov 15 '18 at 18:21
DengueDengue
3814
3814
Does the download resume when the connection comes back online?
– Gordon
Nov 15 '18 at 18:56
See this answer for how to detect drops.
– Gordon
Nov 15 '18 at 19:03
Thank you very much, I will try this approach. Actually downloading does not resume in my case
– Dengue
Nov 16 '18 at 6:17
add a comment |
Does the download resume when the connection comes back online?
– Gordon
Nov 15 '18 at 18:56
See this answer for how to detect drops.
– Gordon
Nov 15 '18 at 19:03
Thank you very much, I will try this approach. Actually downloading does not resume in my case
– Dengue
Nov 16 '18 at 6:17
Does the download resume when the connection comes back online?
– Gordon
Nov 15 '18 at 18:56
Does the download resume when the connection comes back online?
– Gordon
Nov 15 '18 at 18:56
See this answer for how to detect drops.
– Gordon
Nov 15 '18 at 19:03
See this answer for how to detect drops.
– Gordon
Nov 15 '18 at 19:03
Thank you very much, I will try this approach. Actually downloading does not resume in my case
– Dengue
Nov 16 '18 at 6:17
Thank you very much, I will try this approach. Actually downloading does not resume in my case
– Dengue
Nov 16 '18 at 6:17
add a comment |
1 Answer
1
active
oldest
votes
I found the solution which is appropriate for myself.
here is the code which helps me to set timeout on such a long request.
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err))
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So the part that I was looking for is:
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
on the request stream.
So timeout event will be raised after 30 seconds of idle on socket
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325676%2fhow-to-catch-internet-disconnection-event-while-downloading-a-big-file-in-node-j%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found the solution which is appropriate for myself.
here is the code which helps me to set timeout on such a long request.
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err))
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So the part that I was looking for is:
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
on the request stream.
So timeout event will be raised after 30 seconds of idle on socket
add a comment |
I found the solution which is appropriate for myself.
here is the code which helps me to set timeout on such a long request.
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err))
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So the part that I was looking for is:
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
on the request stream.
So timeout event will be raised after 30 seconds of idle on socket
add a comment |
I found the solution which is appropriate for myself.
here is the code which helps me to set timeout on such a long request.
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err))
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So the part that I was looking for is:
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
on the request stream.
So timeout event will be raised after 30 seconds of idle on socket
I found the solution which is appropriate for myself.
here is the code which helps me to set timeout on such a long request.
const request = require('request')
request(
uri: `$pathToRemoteFile`,
encoding: null
).on('error', err => console.log(err))
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
.pipe(fs.createWriteStream(`$pathToStoreFileLocally`))
.on('finish', () => console.log('yeah, successfully downloaded') )
.on('error', err => console.log(err))
So the part that I was looking for is:
.on('socket', socket =>
socket.setTimeout(30000);
socket.on('timeout', () =>
//handle disconnect
)
)
on the request stream.
So timeout event will be raised after 30 seconds of idle on socket
answered Nov 25 '18 at 5:25
DengueDengue
3814
3814
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325676%2fhow-to-catch-internet-disconnection-event-while-downloading-a-big-file-in-node-j%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does the download resume when the connection comes back online?
– Gordon
Nov 15 '18 at 18:56
See this answer for how to detect drops.
– Gordon
Nov 15 '18 at 19:03
Thank you very much, I will try this approach. Actually downloading does not resume in my case
– Dengue
Nov 16 '18 at 6:17