Express & WebSockets - wss connection failed.

bugnode0xFREE

a year ago

Hi There! I've been trying to solve a problem where my websockets seem to close as soon as I refresh the page. I'm using socket.io and I have made sure I configured CORS correctly to avoid any CORS issues. I encountere the following error with no extra explanation from my developer console as you can see in the picture:

WebSocket connection to 'wss://noisse-backend-development.up.railway.app/socket.io/?EIO=4&transport=websocket&sid=BsqSmDCmf4FtbctuAABQ' failed: 

0 Replies

bugnode0xFREE

a year ago

I'm using the railway domain for my development version as also a custom domain configured with railway. This is my current code snippet for my websocket implementation in the backend.

const express = require('express');
const cors = require('cors');
const { createServer } = require('http'); // Ensure this is at the top with other requires
const { Server } = require('socket.io');
const axios = require('axios');
const Redis = require('ioredis');
const userSockets = new Map();

// CONFIG //
require('dotenv').config(); 
const stripe = Stripe(process.env.STRIPE_SECRET_KEY);

const app = express();
const corsOptions = {
  origin: 'https://dev-noisse.vercel.app', // Replace with your frontend domain
  methods: ['GET', 'POST'], // Specify the allowed HTTP methods
  allowedHeaders: ['Content-Type'], // Specify the allowed headers
  credentials: true // Allow sending cookies
};
app.use(cors(corsOptions));
const httpServer = createServer(app);

const io = new Server(httpServer, {
  cors: {
    origin: "https://dev-noisse.vercel.app", 
    methods: ["GET", "POST"],
  },
  pingTimeout: 60000, // Increase the ping timeout to 60 seconds
  pingInterval: 25000 // Send a ping every 25 seconds
});

io.on('connection', (socket) => {
  console.log('a user connected');

  socket.on('authenticate', (hunter_id) => {
    // Validate hunter_id here if necessary
    userSockets.set(hunter_id, socket.id);
  });

  socket.on('disconnect', () => {
    for (const [hunter_id, socketId] of userSockets.entries()) {
      if (socketId === socket.id) {
        userSockets.delete(hunter_id);
        console.log(`User with hunter_id ${hunter_id} disconnected`);
        break;
      }
    }
  });
});
......

const PORT = process.env.PORT || 3001;
  httpServer.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
    io.attach(httpServer);
  });
]

bugnode0xFREE

a year ago

e06a1209-8d68-4c30-a8dc-bee27085cfa7


bugnode0xFREE

a year ago

Any help would be appreaciated ❤️


a year ago

please provide a link to where i could get this error for myself, because failed: is not helpful unfortunaly


bugnode0xFREE

a year ago

I cannot seem to find more information about that error. I'm confused as it does not disclose any extra info.

When you say a link, do you mean a link to my application?


a year ago

yes, a link to where i could get this error for myself


bugnode0xFREE

a year ago

sent via dm


a year ago

is the server and client using the same socket.io version?


bugnode0xFREE

a year ago

Yes, I can confirm both and the client are using the same socket.io version


a year ago

can you make sure the client is only using the websocket transport and that you have an error event listener on both the server and client


bugnode0xFREE

a year ago

yes, let me give it a shot.


bugnode0xFREE

a year ago

I do have error listeners in both the client and the server as also making sure Im using the web socket for transport. I suspect the websocket closes shortly after refreshing for some reason. Is there any way to send a keep alive signal or any other recommendation?


a year ago

i dont see an error event listener in the code you showed me so far, and im also not seeing any client code that is only making use of the websocket transport


bugnode0xFREE

a year ago

I will keep digging. This one is a weird one. Thanks for the help in the meantime.