Any way to fix socket hang up problem?


#1

If i use this code in my project Error socket hang up error show up

const http = require('http');
const express = require('express');
const app = express();
app.get("/", (request, response) => {
  console.log(Date.now() + " Ping Received");
  response.sendStatus(200);
});
app.listen(process.env.PORT);
setInterval(() => {
  http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 280000);

But removing it makes my project damn slow!! I used other way to keep my project like using websocket. But still it is not working as expected it is so slow.

EDIT BY ADMIN: I modified your post so that the code is well formatted.


#2

If you’re using express, why are you making a http (node.js http) server?


#3

I found it online and it helps my project to run fast. So i am using it.


#4

Try to respond something when you get the ping:

app.get("/", (request, response) => {
  console.log(Date.now() + " Ping Received");
  response.end("OK");
});

#5

Oh I’m stupid, your minified code made me confused and I thought you used http for the server and not for pinging


#6

I will try
Thanks sir!
It’s fixed for now. May it(this bug) never come back!


#7

Sure, it did help you out. By the way, it wasn’t a bug. :man_facepalming:


#8

Did you can check my project it’s again here . If you can help me by making it restart when this error occurs i will be helped so much!:smiley:


#9

Anyone can help me now?


#10

This is happening to me too :frowning:


#11

Did you found a fix?


#12

Hello @ArunKapil01

Your node.js app needs something to keep it alive, some sort of event that has to happen in next tick. For example when you’re using an interval to keep it alive which makes the node.js process always have a task to do.

Here’s an example of an app that will live forever:

// Create an interval to keep the process alive.
const interval = setInterval(() => {
  // So, the deal is, nothing really needs to happen here.
}, 3600*24*365)

console.log('Cat', 'Dog', 'Bird')
console.log('Whatever, this app is immortal.')

#13

That’s actually not true. Your website address needs to be pinged every 5 minutes. My program would always be doing something but it cannot stay alive.


#14

You can use uptimerobot


#15

This doesn’t made my project live!


#16

Hello @lieuwe_berg

Well, would you please explain to me why I have a discord bot, that does not host any http nor https servers, nor does it makes any http (or https) requests to any platform except Discord.

Anyways, if you guys have this issue, that a http server must be hosted, this just might be the very shortest fix.

require('http').createServer(async(r,s)=>s.end('ok')).listen(3000);setInterval(()=>{require('http').get(`http://${process.env.PROJECT_DOMAIN}.glitch.me`)},60*5*1000)

#17

:man_shrugging: I have to do it that way.


#18

Using setInterval() to keep a bot awake isn’t recommended as it will not be restarted if we have to stop projects. Providing an end-point to ping is a better approach.


#19

This doesn’t gonna help anyway to fix Socket hang up what’s causing this again.


#20

i found https://cron-job.org/en/ works well. You make an account and make a new cron job. You set the adress as (projectname).glitch.me . you set the shedule to every 5 min (or less) and create cronjob (https://gyazo.com/2fb8f181517822cae4619cab82b58e1d) You don’t need an html file on your project