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

This doesn’t make project live instead it makes the project running but without live icon​:joy::joy:

1 Like

I have it loading but all my bots runinning still :laughing: (ignore the main man. I didn’t make him)
https://gyazo.com/5e689f01e2d020b707bd002927c62d85

That doesn’t gonna help i know that is mee6(main) my problem is socket hang up and that thing you’re talking about is pinging bot every 5 minutes is not make your glitch app live making your discord app online doesn’t make your app liveScreenshot_2018-06-06-09-27-08 This is the live thing , i am talking about so don’t give your ideas. I am already using uptimerobot to ping my bot every 5 minutes . Read my question properly before talking about anything!
Your bot is still on loading so they will be damn slow!

1 Like

The code above will host a http server on port 3000 (outpus at 80 on {project}.glitch.me), it pings the http server every 5 minutes.

1 Like

To get the live icon, your project needs to respond successfully (200 OK) to an HTTP request on port 3000.

Then how about

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

Using setInterval() to keep a bot awake isn’t recommended as it will not be restarted if we have to stop projects. Using an external service like uptimerobot to request an end-point would be a more reliable solution.

1 Like

Ah, right. Cool :+1:

This doesn’t even fix my issue idk why i am always getting error socket hang up please fix this glitch @Tim

1 Like

I suggest adding catch blocks around that error so you have some more info to work on as to what is causing the socket to hang up.

This is happening during start of project i am already using catch but that doesn’t help and i don’t know why this is happening. Maybe this is due to request limit or something?

1 Like

What error do you get in the catch? Request limits return a 429 error response.

This returns error from discordapp.com

1 Like

What’s the error text from discord?

That’s a long error and it gone because i can’t see old log :disappointed::disappointed::disappointed:

1 Like

` _pendingData: null,

    _pendingEncoding: '',

    server: undefined,

    _server: null,

    ssl: null,

    _requestCert: true,

    _rejectUnauthorized: true,

    parser: null,

    _httpMessage: [Circular],

    read: [Function],

    _consuming: true,

    write: [Function: writeAfterFIN],

    _idleNext: null,

    _idlePrev: null,

    _idleTimeout: -1,

    [Symbol(asyncId)]: 27,

    [Symbol(bytesRead)]: 0 },

 connection: 

  TLSSocket {

    _tlsOptions: [Object],

    _secureEstablished: false,

    _securePending: false,

    _newSessionPending: false,

    _controlReleased: true,

    _SNICallback: null,

    servername: null,

    npnProtocol: null,

    alpnProtocol: null,

    authorized: false,

    authorizationError: null,

    encrypted: true,

    _events: [Object],

    _eventsCount: 9,

    connecting: false,

    _hadError: true,

    _handle: null,

    _parent: null,

    _host: 'discordapp.com',

    _readableState: [Object],

    readable: false,

    domain: null,

    _maxListeners: undefined,

    _writableState: [Object],

    writable: false,

    allowHalfOpen: false,

    _bytesDispatched: 279,

    _sockname: null,

    _pendingData: null,

    _pendingEncoding: '',

    server: undefined,

    _server: null,

    ssl: null,

    _requestCert: true,

    _rejectUnauthorized: true,

    parser: null,

    _httpMessage: [Circular],

    read: [Function],

    _consuming: true,

    write: [Function: writeAfterFIN],

    _idleNext: null,

    _idlePrev: null,

    _idleTimeout: -1,

    [Symbol(asyncId)]: 27,

    [Symbol(bytesRead)]: 0 },

 _header: 'GET /api/v7/gateway HTTP/1.1\r\nHost: discordapp.com\r\nAuthorization: MzU4Nzg1MDIzMjg2OTY4MzIz.Db-Gsg.gyIt2ajHXLKqHDxlJz1LvqRjmS4\r\nUser-Agent: DiscordBot (https://github.com/discordjs/discord.js, 12.0.0-dev) Node.js/v8.9.4\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n\r\n',

 _onPendingData: [Function: noopPendingOutput],

 agent: 

  Agent {

    domain: null,

    _events: [Object],

    _eventsCount: 1,

    _maxListeners: undefined,

    defaultPort: 443,

    protocol: 'https:',

    options: [Object],

    requests: {},

    sockets: [Object],

    freeSockets: {},

    keepAliveMsecs: 1000,

    keepAlive: true,

    maxSockets: Infinity,

    maxFreeSockets: 256,

    maxCachedSessions: 100,

    _sessionCache: [Object] },

 socketPath: undefined,

 timeout: undefined,

 method: 'GET',

 path: '/api/v7/gateway',

 _ended: false,

 res: null,

 aborted: undefined,

 timeoutCb: null,

 upgradeOrConnect: false,

 parser: null,

 maxHeadersCount: null,

 [Symbol(outHeadersKey)]: 

  { host: [Array],

    authorization: [Array],

    'user-agent': [Array],

    'accept-encoding': [Array] } } }

:apple::waxing_crescent_moon: Your app is listening on port 3000

[2018-06-10 16:07:58] Loading a total of 160 commands.

[2018-06-10 16:07:59] Loading a total of 12 modules.

[2018-06-10 16:07:59] Loading a total of 12 events.

`

1 Like

Here is the error @Gareth

It looks like you missed the start of the error, the JSON is incomplete.

@ArunKapil01Is it a Discord Bot? Ifso, I can help…

And to get a project that also lives forever without a code, use uptimerobot.com.

First of all, because of the massive error I suggest you privatly message me your package.json file so I can see if it is in there.

But because it’s nothing important you also may post it in here if you would like, your choice!

~Rienknr1

I am already using that and socket hang up error is still here

1 Like

Here is my package. json

{
  "name": "Pikachu",
  "version": "1.0.2",
  "description": "A Discord Bot written in Discord.js",
  "engines": {
    "node": "10.x",
    "npm": "4.2.0"
  },
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "canvas": "^2.0.1",
    "canvas-constructor": "^1.1.2",
    "chalk": "^2.4.1",
    "cheerio": "^0.22.0",
    "dblapi.js": "^2.2.0",
    "discord.js": "github:discordjs/discord.js#",
    "ejs": "^2.6.1",
    "enmap": "3.x",
    "enmap-mongo": "^2.0.2",
    "express": "^4.16.4",
    "fs": "^0.0.2",
    "fs-nextra": "^0.3.7",
    "he": "^1.2.0",
    "idiotic-api": "^0.2.11",
    "moment": "^2.22.2",
    "moment-duration-format": "^2.2.2",
    "prism-media": "^0.3.1",
    "random-puppy": "^1.1.0",
    "request": "^2.88.0",
    "return-deep-diff": "^0.3.0",
    "snekfetch": "^4.0.4",
    "superagent": "^3.8.3",
    "urban-dictionary": "^2.2.1",
    "weather-js": "^2.0.0",
    "wikifakt": "^1.0.3",
    "xml2js": "^0.4.19",
    "node-fetch": "^2.2.0"
  },
  "keywords": [
    "node",
    "heroku",
    "express"
  ],
  "license": "MIT"
}

MOD EDIT: cleaned up code formatting

1 Like

Here is the error

    Possibly Unhandled Rejection at: Promise Promise {8:12 AM

<rejected> { Error [WS_CONNECTION_TIMEOUT]: The connection to the gateway timed out.8:12 AM

at Timeout.setTimeout (/rbd/pnpm-volume/5c4f3043-faa8-4f56-8ee3-4da9829839c8/node_modules/.github.com/discordjs/discord.js/3418b5a1a2e2e424369da42f0a04dc3523f3d933/node_modules/discord.js/src/client/Client.js:259:16)8:12 AM

at ontimeout (timers.js:436:11)8:12 AM

at tryOnTimeout (timers.js:300:5)8:12 AM

at unrefdHandle (timers.js:520:7)8:12 AM

at Timer.processTimers (timers.js:222:12) [Symbol(code)]: 'WS_CONNECTION_TIMEOUT' } } reason: { Error [WS_CONNECTION_TIMEOUT]: The connection to the gateway timed out.8:12 AM

at Timeout.setTimeout (/rbd/pnpm-volume/5c4f3043-faa8-4f56-8ee3-4da9829839c8/node_modules/.github.com/discordjs/discord.js/3418b5a1a2e2e424369da42f0a04dc3523f3d933/node_modules/discord.js/src/client/Client.js:259:16)8:12 AM

at ontimeout (timers.js:436:11)8:12 AM

at tryOnTimeout (timers.js:300:5)8:12 AM

at unrefdHandle (timers.js:520:7)8:12 AM

at Timer.processTimers (timers.js:222:12) [Symbol(code)]: 'WS_CONNECTION_TIMEOUT' }8:12 AM

a few seconds ago

1 Like

Based on a quick review it looks like a built-in discord.js process is trying to connect to some url and is timing out. I’d probably start by verifying every url the discord client tries to connect to and disabling them one by one to see if you can make the error go away.

You could try to narrow it down first by determining which connections the client makes use a timer in their process - not knowing a ton about discord.js I’m not sure if that will actually narrow anything down.

1 Like

It doesn’t work now :frowning:

1 Like

@cori can you tell me how you fixed my bot?

1 Like

Guys, it’s ridiculously simple. You shouldn’t use glitch to host discord bots that are used a lot of times per day. Yes, I host bots here, but simply switching to a real VPS solves this problem. Internet is limited, and thus you stop recieving responses. I had this with my old VPS. A lot. Upgrading the internet solved this problem completely. VPS’s don’t have to be expensive. You can get the same things that glitch has with unmetered internet for $1,66 per month ($20/year).

That doesn’t gonna help me

1 Like

“simply switching to a real VPS solves this problem.”

Or try making less requests somehow.

Well if i can afford a real vps i never used glitch in first place also second about request then idk what is causing that issue! You’re not really helping in this issue!

1 Like

What kind of discord bot do you have? Describe very detailed.

That’s really off topic now ! @lieuwe_berg

1 Like

Hi everyone,

let’s try to keep a meaningful and productive conversation, without blaming at each other. Moreover, yeah, you can always switch to something else, but we’ll do the possible to keep everyone on Glitch, if we can :slight_smile:

@ArunKapil01, I think that @lieuwe_berg question is on the right path to help you with your issue. Socket hangup problems are difficult to track down and resolve, so it’s important to know what your discord bot does, otherwise we can’t help if not with very general suggestions.

Here comes one of the general suggestions :stuck_out_tongue: Try to find the place that generates this issue, and try to catch the exception. Then you can print more detailed information about the request that failed, and you might eventually come to a solution.

That’s what I was aiming for yeah. To add, describe what every command does. If it just sends, or plays music, or posts to hastebin etc.

https://pikacord.blogspot.com/2018/10/pikacord-src-httpsbotsfordiscord.html?m=1 here is full list of info @lieuwe_berg

1 Like

And also music and two commands which is on other project works properly only pikachu project not works

1 Like

I see that it’s in 1774 servers, is that correct?

Yeah i purge many it was working even when it was in 2300 guilds. I purged because i thought that guilds may be the issue but that doesn’t help!

This is more or less what I was talking about but @etamponi said it much better:

One way to isolate where the error comes from is to disable features one at a time and observe things, seeing if the socket timeout stops. If it does then you know what’s causing it and you can either remove that feature or dig deeper to try to solve the problem somehow. But there’s no one-size-fits-all answer - it’s going to be very specific to your bot and is going to take careful, painstaking work that no one else is likely to be able to do for you to find and sort.

And to answer your previous question @ArunKapil01 I didn’t change anything when it started to work earlier, so something changed elsewhere, at least temporarily. That’s another possible line on the problem; what could possibly have changed in that timespan that would affect the bot’s operation without any code change? You could start that line of investigation by listing all the possible things that could have changed there and looking into them one by one.

You don’t have enough traffic availablity for that amount of servers. Socket hang up occurs on discord bots when this issue is present. You probably also don’t have enough ram, but that doesn’t produce this error.

I’m speaking from experience. There’s nothing wrong with your code as the error originates from the package. If it was something in your code, you’d get a different stack error.

Get a VPS, they’re not expensive. I like glitch - I love it -, but it cannot handle such discord bot. Asking for donators or having donator-only commands will help you cover the funds. Patreon works perfectly, but donate bot or simple PayPal donations work fine too.

Thanks for your suggestion but i don’t have a PAN card yet and don’t even have a credit card i am student and i don’t have that even i pay only through cash so i don’t think i can get a vps now or later!
Also my code have issue for sure( I don’t use async for my code i am planning to switch to eris) , as my other project ( music , cleverbot and pokedex commands working properly, They also have a lot of traffic as they have same bot token) and my old friend bot which is in 2400 guilds hosted on glitch works perfectly too!

1 Like

Also about patreon thing then i don’t think anyone gonna support my bot even after 12hrs nobody came asking that why bot is down or something :slightly_frowning_face:

1 Like

You don’t need a VPS @ArunKapil01,

The only issue that is there is that Discord.js cannot connect to the Discord Server…

So make sure you have a valid Token and that there are no errors in your scripts.

Let me know if this helped. (Probably not, but I will give it a try)

It’s glitch’s internet limitations. You indeed don’t need a VPS, but usually these have more internet available.

And that’s why I am using Uptimerobot. My bot also runs on Glitch… And I got no errors…

~Rienknr1

I also host bots on glitch, it works wonderfully. Though, how big is your bot? Do you only use it in one or two servers or is it in 1000 servers?

If in two, then you indeed won’t be experiencing problems.

If in 1000, you will get errors (eventually) saying “socket hang up” or “econnreset”.