WebSocket not working

When I try to connect wss://<my-domain>.glitch.me/, it responds with status code 502. I have tried to add a User-Agent field and an Origin field in the header, but it doesn’t work. Still 502. I’m about to get mad.

One more thing, if I connect it from my browser (using devtools), it works. However, I need to connect it using another client.

Also not work when using custom domain. Neither browser nor client.

Hi @wuxianucw, welcome to the community :slight_smile:

I’m not sure what your problem is (can you share any code?) but I do have a working WebSockets app that I can show you.

It is a basic “push-to-talk” radio system: https://glitch.com/edit/#!/sg-ptt

If you search the project with Ctrl+Shift+F for ‘wss’ you can find the usages:

This may help you diagnose differences between your project and this one that works. Feel free to remix it.

I hope it helps!


Thanks for your share!

I think using WebSocket in a browser will work, but a third-party client won’t. My connect code is like this:

const WebSocket = require('ws');

const ws = new WebSocket('wss://morning-mountainous-mist.glitch.me/', {
    headers: {
        "Authorization": "secret",
        "Origin": "https://morning-mountainous-mist.glitch.me",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
    origin: 'https://morning-mountainous-mist.glitch.me',

ws.on('open', () => {
    console.log('WebSocket opened');
.on('message', (data) => {

At wss://morning-mountainous-mist.glitch.me/, I’m running a WebSocket server. So this will connect to it, or got a 401 error due to the wrong Authorization field. But I got neither. My server cannot receive the request. I guess it was blocked by Glitch’s API gateway.

Thank you again! I’m new here and still exploring.

1 Like

Ok, I see.

I found this reply in another topic:

So, apparently, “Only the port from process.env.PORT will be open outside of the container”

Can you set your PORT in .env and try this?

(Topic: Error during WebSocket handshake: Unexpected response code: 502 - #2 by cori)

Oh, I’m sure that my port is OK. I have read that topic, but no help. I can connect it using browser JavaScript WebSocket API, so my server and config should be right. I just can’t do so outside the browser. I’m quite puzzled.


could you connect those same browser and node clients to a dummy server that can dump out the request headers/etc? maybe we could look at what differs and try to reconcile them