Nodejs WebSocket Server

this is the server: https://glitch.com/edit/#!/testwsserver?path=server.js%3A15%3A65

const express = require("express");
const app = express();
const http = require('http');
const server = http.createServer(app);

server.listen(3000);
setInterval(() => http.get('http://localhost:3000'),1000*60*5);

app.get("/", (req, res) => res.send('x'));

// WebSocket
const WebSocket = require('ws');
const wss = new WebSocket.Server({server});

wss.on('connection', (ws, req) => console.log('new connection'));

this is the client: https://glitch.com/edit/#!/testwsclient?path=server.js%3A8%3A81

// WebSocket
const WebSocket = require('ws');

const ws = new WebSocket('wss://testwsserver.glitch.me');
ws.on('open', () => console.log('open'));
ws.on('error', (err) => console.log(err)); // -> 502 error code everytime

i get error code 502 everytime when try to connect to the server and i dont know why.

using js Websocket in browser works.

    const ws = new WebSocket('wss://testwsserver.glitch.me/');
    ws.onopen = () => console.log('open');
    ws.onerror = (err) => console.log(err);

    // -> open

This worked yesterday. only since today i’m getting the 502 error

2 Likes

I belive that glitch is having some issues at the moment.

1 Like

I’ve been getting intermittent 502s. I’m working around this by repeating the request, and after several tries, it upgrades the connection successfully.

Thanks for making a report about this. I was wondering if there was a factor other than problems with my code.

1 Like

This line makes your server a pinging service (to itself), so its likely to get blocked. Have you tried without keeping it awake?

Edit - I usually get 502 bad gateway response when the project attempts to wake up, and takes too long.

i can connect by pasting this in chrome console.
new WebSocket('wss://testwsserver.glitch.me/');

I missed this earlier … the client is running on a Glitch server, connecting to the server on another Glitch server?

I expect this setup will be caught by the current block of ping services.

why can i establish a connection to the glitch websocket server using
new WebSocket('wss://testwsserver.glitch.me/'); in chrom console.
i also get 502 when i try to connect with the client running on my machine

1 Like

I’m wondering if a WebSocket connection to a glitch project will wake it up, or do only http(s) requests

That selfping is hillarious, it won’t work xd It needs to go through the proxy to count as a ping :stuck_out_tongue:

Anyway, try moving server.listen below the websocket constructor.

actually it did work.

The localhost ping? 

yes. it got through the proxy.

Nope, it doesn’t:

image

If it went through the proxy, this would have started to give me errors.

const express = require("express");
const fetch = require("node-fetch");

const app = express();

let incomingRequests = 0;
let outgoingRequests = 0;

const sendRequest = () => {
  fetch("http://localhost:3000")
    .then(async response => {
      if (!response.ok)
      {
        console.log("Request failed %s", response.status);
      } else
      {
        outgoingRequests++;
        console.log("Request successful %s", outgoingRequests);
      }
    }).catch(async error => {
      console.error("Request Error: ", error);
    });
};

app.use(async (req, res) => {
  incomingRequests++;
  res.status(200).end("ok");
  console.log("Incoming (%d) okay", incomingRequests);
});

app.listen(3000, () => console.log(":3000"));

setInterval(() => sendRequest(), 50);