Any way to fix socket hang up problem?

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.

3 Likes

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

4 Likes

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

1 Like

Try to respond something when you get the ping:

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

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

3 Likes

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

5 Likes

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

2 Likes

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:

2 Likes

Anyone can help me now?

1 Like

This is happening to me too :frowning:

1 Like

Did you found a fix?

1 Like

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.')
1 Like

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.

You can use uptimerobot

1 Like

This doesn’t made my project live!

1 Like

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)

:man_shrugging: I have to do it that way.

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.

1 Like

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

1 Like

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