Remixing a websocket project not producing same results

Hey so I am trying to create a WebSocket server, so my client (on a different application) can connect to it. I found a simple WebSocket server example and tried connecting to it and it worked. Though, when I remixed the project (and changed nothing) I couldn’t connect to it. I’ve tried so many different ways to create a WebSocket server that will connect to this client, and if they seem to work, and I remix the project or use the code, it never seems to work on its own.

I am really confused because if I launch it using Heroku, it works fine so I know it’s a problem with glitch somehow. If someone could help me that would be great.

Working Project: https://glitch.com/edit/#!/flavio-websockets-server-example
My Remix: https://glitch.com/edit/#!/remix-websocket-test2

1 Like

@cori Do you know what I should do? (I do not know if I should contact glitch support).

If you could help me that would be appreciated.

1 Like

I thought I’d mention that I’m having some similar problems. I can’t seem to get a websocket server of any kind running. All attempts to send messages to the project url (ie wss://project.glitch.me) result in “WebSocket connection failed”.

const express = require('express');
const app = express();

var expressWs = require('express-ws')(app);
 
app.use(function (req, res, next) {
  console.log('middleware');
  req.testing = 'testing';
  return next();
});
 
app.get('/', function(req, res, next){
  console.log('get route', req.testing);
  res.end();
});
 
app.ws('/', function(ws, req) {
  ws.on('message', function(msg) {
    console.log(msg);
  });
  console.log('socket', req.testing);
});
 
app.listen(process.env.PORT);

client:

// Create WebSocket connection.
const socket = new WebSocket('wss://public-sphere.glitch.me/');

// Connection opened
socket.addEventListener('open', function (event) {
    socket.send('Hello Server!');
});

// Listen for messages
socket.addEventListener('message', function (event) {
    console.log('Message from server ', event.data);
});

// Listen for messages
socket.addEventListener('error', function (event) {
    console.log('Error from server ', event);
});

Hi there! If you add a user agent to your request headers, it should work. We block requests when there is no user agent unless you’re a Glitch subscriber. If you’re a subscriber and still having this issue, let us know the name of the project so we can figure out what’s going on.