Express & WebSockets - wss connection failed.

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:
WebSocket connection to 'wss://noisse-backend-development.up.railway.app/socket.io/?EIO=4&transport=websocket&sid=BsqSmDCmf4FtbctuAABQ' failed:
No description
12 Replies
Percy
Percy8mo ago
Project ID: N/A
Sam (caffeine)
Sam (caffeine)8mo 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);
});
]
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);
});
]
e06a1209-8d68-4c30-a8dc-bee27085cfa7 Any help would be appreaciated ❤️
Brody
Brody8mo ago
please provide a link to where i could get this error for myself, because failed: is not helpful unfortunaly
Sam (caffeine)
Sam (caffeine)8mo 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?
Brody
Brody8mo ago
yes, a link to where i could get this error for myself
Sam (caffeine)
Sam (caffeine)8mo ago
sent via dm
Brody
Brody8mo ago
is the server and client using the same socket.io version?
Sam (caffeine)
Sam (caffeine)8mo ago
Yes, I can confirm both and the client are using the same socket.io version
Brody
Brody8mo 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
Sam (caffeine)
Sam (caffeine)8mo ago
yes, let me give it a shot. 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?
Brody
Brody8mo 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
Sam (caffeine)
Sam (caffeine)8mo ago
I will keep digging. This one is a weird one. Thanks for the help in the meantime.
Want results from more Discord servers?
Add your server