Unable to connect to glitch websocket server via nodejs

i’ve tried to connect to the server via my local machine and even another server but i get an “timed out” error, sample code that i’m using to connect:

const { WebSocket } = require("ws");
require("dotenv").config();
let ws = new WebSocket(process.env.masterURL); //masterURL being the websocket server
//masterURL: wss://master-ws-server.glitch.me:3000/

what am i doing wrong?
in the server-side, i’m using express and express-ws to create the server

Use wss://master-ws-server.glitch.me/ instead of wss://master-ws-server.glitch.me:3000/

2 Likes

After doing some testing, you may get a 502: Bad Gateway error when connecting to the WebSocket server running on the glitch project. This doesn’t occur when connecting from a web browser.

and my question is:how can i connect to a glitch server via nodejs, since there is an error.
I’ve confirmed what you said tho, same error.

You can connect to a glitch project via nodejs by adding the User-Agent header so it won’t give a 502: Gateway Error.

const { WebSocket } = require("ws");
require("dotenv").config();
let ws = new WebSocket(process.env.masterURL, {
  headers: {
    "User-Agent": `WebSocket Client`
  }
}); //masterURL being the websocket server
//masterURL: wss://master-ws-server.glitch.me:3000/

The User-Agent header can be changed to anything you want to it to be.

1 Like

i still get the ETIMEDOUT error tho…

for the masterURL, do wss://master-ws-server.glitch.me/ instead of wss://master-ws-server.glitch.me:3000/ - By adding the port to a glitch project url, it will not work.

I forgot to remove :3000 in my previous reply at the end. I copied and pasted the code in your initial post, and added the headers part. Sorry for the mistake.

1 Like

works! thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.