ArduinoWebsockets icon indicating copy to clipboard operation
ArduinoWebsockets copied to clipboard

Getting my connection closed when on node js wss server

Open dederomagnolo opened this issue 1 year ago • 4 comments

Hi all and @gilmaimon. First of all, thanks for this lib and for all the support for the community. This is very useful and I could learn A LOT about websockets reading the discussions here.

Describe the bug I have a problem on my system with node js wss server + esp8266 clients.

my clients enter in a kind of loop when the server is authenticating them and immediately the connection is closed. I can see the close event only on client side and it is returning 1002 (protocols errors).

Technical settings:

  • I have a node JS (14.5.0) server exposing the wss with the lib ws. I connect my esp8266 clients using ArduinoWebsockets (0.5.3) to it.
  • ESP8266 lib version: 3.0.2
  • I noticed that is occuring because of async calls on my authentication function on server side... but I already handled everything to work properly and I am able to test and get success using a web client.
  • My server is live via Render cloud and this error only happens live.
  • From my esp8266 client, I am calling the function below to connect to the server (minimal code) on my loop:
void handleWebsocketConnection(bool startup) {
 // first connection on setup ( )
  if (startup) {
    bool wsConnected = wsclient.connect(SERVER_URI);

    if (wsConnected) {
      serialDebug("Connected with BeThere websocket server:");
      serialDebugLn(SERVER_URI);
    }
    lastConnectionTentative = millis();
  }
  
  if (wsclient.available()) {
    wsclient.poll();
  } else {
   // if the connection is closed, try to reconnect
    bool shouldTryToReconnect = (millis() - lastConnectionTentative) > connectionToWsTimeout;

    if (shouldTryToReconnect) {
      serialDebugLn("...Reconnecting to websocket server");

      wsclient = {}; // This will reset the client object
      wsclient = WebsocketsClient();
      wsclient.onEvent(onEventsCallback);
      wsclient.onMessage(onMessageCallback);
      
      bool wsConnected = wsclient.connect(SERVER_URI);
  
      if (wsConnected) {
        serialDebug("Reconnected with BeThere websocket server:");
      }

      lastConnectionTentative = millis();
    }
  }
}

My suspicion is that my esp8266 client is not reacting well to this authentication function cause I am not handling it correctly because when I pull off one of the async calls I do, they are able to connect and to keep the connection estable. I tried to add this timer shouldTryToReconnect to make the client wait 5 seconds before another tentative but without success.. Can you help me to understand what I am doing wrong?

dederomagnolo avatar Jun 27 '24 13:06 dederomagnolo

hello may i ask what format of websocket did you use in render? in local my websocket in working but on render, i cant seem to connect my nodejs server

sencin avatar Jul 12 '24 03:07 sencin

Hey @Sencin, you can share the error you getting, more easy to debug. The only thing I remember, in the beggining I had connection problems cause I tried to connect to the host using ws:// instead of wss://. Render interprets it as an insecure connection.

dederomagnolo avatar Jul 12 '24 12:07 dederomagnolo

Does it need a SSL since it's a secure connection. Or does render already have SSL for Web sockets?

sencin avatar Jul 12 '24 14:07 sencin

I think they already have. Not clear for me too, but take a look on this thread on their community: https://community.render.com/t/connect-via-websockets/8355/3

and share if you get any success.

dederomagnolo avatar Jul 12 '24 14:07 dederomagnolo