0

I have a NodeJS express app which listens on port 3000. In that app I define a websocket connection like so:

const express = require('express');
const app = express();
const server = require("http").createServer(app);


const WebSocket = require("ws");
const wss = new WebSocket.Server({ server });

wss.on("connection", ws => {
    ws.send("You (the client) connected");
    ws.on("message", msg => {
        ws.send("Server received your msg: " + msg);
    });
});
app.listen(3000, () => {console.log("Listening on port 3000");});

This code is on the NodeJS backend, and it listens for websocket connections. It sends a message to the client when the client connects, and when the client sends a message.

On the font end, I have the following vanilla JavaScript code inside my index.html:

const socket = new WebSocket("ws://my.url.com:3000");

socket.addEventListener("open", evnt => {
    console.log(evnt);
    socket.send("Меssage from client");
});


socket.addEventListener("message", evnt => {
    console.log("Received msg from server:", evnt.data);
});

But my code does not work: when I run the front-end code, the socket object (in the front end) never connects; when I try to call socket.send I get an error:

Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

Eventually the connection times out but the client socket never connects to the socket on the server. How can I fix my code so that the client side can connect successfully?

Krokodil
  • 1,165
  • 5
  • 19
  • Does this answer your question? [Express.js - app.listen vs server.listen](https://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen) – gre_gor Sep 12 '22 at 19:45
  • @gre_gor I followed that advice, and I got another error: `Uncaught DOMException: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.`. To fix this, I changed `ws://` to `wss://` in my client side, and `const server = require("http")` to `const server = require("https")` in my server side. I tried again with that code, but I got the same error as I got originally (`Could not connect to server`). Am I doing something wrong again? – Krokodil Sep 13 '22 at 17:54

1 Answers1

0

You can use socket.readyState to check what state the server is in.

There are four possible states: CONNECTING, OPEN, CLOSING, or CLOSED. So if you used a function like this:

window.setInterval(function() {
    if(socket.readyState == 'OPEN') {
         //send while open
    }
}, 500)

Then that should do the trick. Read more about it here

Owen Odroski
  • 35
  • 1
  • 8
  • Thank you for answering. I recently realised that the reason it didn't work was because the server that I am using doesn't even support websockets! – Krokodil Dec 06 '22 at 18:03
  • I am currently having trouble with a WebSocket and I don't know what's wrong with it. What server are you using so maybe I can answer my own question? – Owen Odroski Dec 06 '22 at 19:28
  • It's called Cyclic, here's the link: https://www.cyclic.sh/ – Krokodil Dec 07 '22 at 18:02
  • 1
    Ok, it's not the same things I'm using but that's a really interesting program! – Owen Odroski Dec 07 '22 at 19:10