My app cannot establish websocket connections

As a Render first-time user, I am experiencing difficulties establishing a secure WebSocket connection

I have an Express server running my Node.js app, which is listening on port 3000. Here’s the code I’m using to start the server:

const app = express();
app.listen(3000, () => {
console.log(Server listening on port 3000);
});

After that, I set up a WebSocket server using Node.js’ ‘ws’ library:

const WebSocket = require(‘ws’);
const server = new WebSocket.Server({ port: 3001 });

To connect, on the client side, I specify wss instead of ws as Render requires it. Here is the code I used to initiate the connection:

const socket = new WebSocket(‘wss://my.example.com:3001’);

However, upon trying to use the socket, the logs show that the client connection is never opened and closes immediately.

I also attempted to set up a WebSocket server from the server side using the following code:

const https = require(‘https’);
const serverws = https.createServer(options);
serverws.listen(3001, () => {
console.log(‘WS Server started on port 3001’);
});
const server = new WebSocket.Server({ server: serverws });

However, the result was the same as before, where the client connection was never opened and closed right away. I also tried using .pem files for the setup, but it did not resolve the issue:

const options = {
key: fs.readFileSync(‘key.pem’),
cert: fs.readFileSync(‘cert.pem’)
};
const serverws = https.createServer(options);
serverws.listen(3001, () => {
console.log(‘WS Server started on port 3001’);
});
const server = new WebSocket.Server({ server: serverws });

Could you kindly provide any guidance on how to connect websockets on Render? I have tried setting up a nodejs server and a websocket server but the connection is never opened and the socket closes right away. I have also tried setting up an https server but to no avail. Is there anything I am missing or any configuration I need to change? Any help or guidance would be greatly appreciated.

Hi @voxv,

Are you still having issues with your websockets here?

If so, could you follow the steps in https://socket.io/docs/v3/troubleshooting-connection-issues/ and send over any debugging logs you can capture?

Thanks,

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