Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting my connection closed when on node js wss server #163

Open
dederomagnolo opened this issue Jun 27, 2024 · 4 comments
Open

Getting my connection closed when on node js wss server #163

dederomagnolo opened this issue Jun 27, 2024 · 4 comments
Assignees

Comments

@dederomagnolo
Copy link

dederomagnolo commented Jun 27, 2024

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?

@sencin
Copy link

sencin commented Jul 12, 2024

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

@dederomagnolo
Copy link
Author

dederomagnolo commented Jul 12, 2024

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.

@sencin
Copy link

sencin commented Jul 12, 2024

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

@dederomagnolo
Copy link
Author

dederomagnolo commented Jul 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants