Access websocket on glitch with wscat

I created a simple websocket service which just echos back everything put into it. I can access this service with the WebSocket API from Firefox and Chrome. When I access it through a nodejs script (which uses websocket/ws) or e.g. the wscat tool available on Linux to write an automated test, I get a 502 response. If I put the exact same service on a private server or also heroku, then my local nodejs scripts and wscat works. From my perspective it looks like glitch.me is doing something different, but I’m not sure if this is something I can fix from my side.

Can you try opening the script over the terminal as a test?

wscat -C wss://url

Neither of the following two work, both result in a 502 (while using a WebSocket within a browser works)

wscat -c ws://url
wscat -c wss://url

This happens from a local terminal on my computer, but also from the terminal within glitch.me (npm install wscat)

You know I used wss://url as an example right :laughing:

Yes absolutely, I’m not sure if it is a good idea to post the glitch url here. The source code is trivial:

server.js

const ws = require('ws'); // https://github.com/websockets/ws
const http = require('http');
const PORT = process.env.PORT;
const server = http.createServer((req, res) => {
    if (req.method === 'GET') {
        res.end('js-broadcast\n');
    }
});
const wsserver = new ws.Server({server});
wsserver.on('connection', (ws, req) => {
    console.log('connection', req.connection.address());
    ws.on('message', data => {
        wsserver.clients.forEach(client => {
            const isOpen = client.readyState === ws.OPEN;
            if (isOpen) {
                client.send(data);
            }
        });
    });
});
server.listen(PORT);

package.json

{
  "version": "0.0.1",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "ws": "7"
  },
  "engines": {
    "node": "12.x"
  }
}

Actually, this may be an issue on Glitch’s side. See the error logs in this post:

For me the funny thing is that the WebSocket within Firefox or Chrome works fine. Just the command line tools (my own or wscat) all fail. This could also be a problem of the implementation of all this tools and/or libraries. Maybe I should try to test completely different implementation from other languages or if nothing helps, dig down deeper myself.

I found several topics on support.glitch.com related to websocket and 502, but I was unable to connect any of this issues to this specific case.

I now also tried the websockets (s at the end) module for python3, which seems to work. I’ve to assume then that at least the websocket/ws library (which seems to be also used by wscat) and also has an issue (also socketio seems to have a problem).

#!/usr/bin/env python3

import asyncio
import websockets  # pip3 install websockets

async def hello():
    url = 'ws://<url>'
    async with websockets.connect(url) as websocket:
        await websocket.send('Hello From Python')
        response = await websocket.recv()
        print(response);

asyncio.get_event_loop().run_until_complete(hello())

I hope this may help someone else running into the same issue (Nov 2020).

1 Like

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