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.

Fixed, thanks

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.

1 Like

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