JS library connect-disconnect loop

So I was investigating more on this: https://github.com/vshymanskyy/blynk-library-js/issues/40
And what is happening in my case is that sometimes it enters a loop where the tcp socket receives a msg_id == 0, so it disconnects and tries to reconnect and receive again a msg_id == 0 and so on in a loop. If using SSL it leads to crash after a while.

This is the code in blynk.js (line 397) that makes the socket disconnect:


if (msg_id === 0)  { return self.disconnect(); }

Any idea on what the msg id == 0 is (BLYNK_CMD_RESPONSE), why it is received after connection and why the library disconnects when receiving it?
Can I safely remove that disconnect in the JS library?

Thanks for reporting. Can you provide the full code listing of your script? I’ll look into the issue

I can’t the full code (it’s 2 scripts actually and 9000+ lines and there is some proprietary code). I can try to extract the blynk related code, is it ok?

From blynk library c++

File: https://github.com/blynkkk/blynk-library/blob/master/src/Blynk/BlynkProtocol.h

line 239 in function “processInput”

if (ret < 0 || hdr.msg_id == 0) {
#ifdef BLYNK_DEBUG
        BLYNK_LOG2(BLYNK_F("Bad hdr len: "), ret);
#endif
        return false;
    }

Line 154 on function “run”

    if (conn.connected()) {
        while (avail || conn.available() > 0) {
            //BLYNK_LOG2(BLYNK_F("Available: "), conn.available());
            //const unsigned long t = micros();
            if (!processInput()) {
                conn.disconnect();
// TODO: Only when in direct mode?
#ifdef BLYNK_USE_DIRECT_CONNECT
                state = CONNECTING;
#endif
                BlynkOnDisconnected();
                return false;
            }
            avail = false;
            //BLYNK_LOG2(BLYNK_F("Proc time: "), micros() - t);
        }
    }

From what we see from the C ++ library the msg_id == 0 is equivalent to having received a “bad header” and, if connected, forces the disconnection.

In my library for node-red I do not have this control yet everything works.

The interesting question is why do you get a message with id 0?
Do you use the blynk cloud or local server?
Could you try to log the messages of the protocol that you receive and send ??

best regards
Gabriele

1 Like

Thanks. Msg ID and Command Type are indeed different things :wink:

So what does it mean? :slight_smile: