Node Red losing websocket connection

For some reason, Node-Red has been having trouble maintaining a connection to HA since 0.10x.x series. I don’t know exactly when it started, since I don’t pay much attention to the automations until I realize they’ve stopped working – the only automation I have is when the husband and I leave our home, and HA goes into an away state.

Any idea why it’s having trouble keeping a connection?

Times are in Mountain Time. It went on/off a dozen times until it finally disconnected and never reconnected:

31 Dec 10:58:22 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
31 Dec 11:03:04 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
31 Dec 11:03:22 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
31 Dec 11:08:04 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
31 Dec 11:08:23 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
31 Dec 11:13:04 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
31 Dec 11:13:14 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
31 Dec 11:18:04 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant

My configuration is:

{
  "credential_secret": "node-red-secret",
  "dark_mode": true,
  "http_node": {
    "username": "",
    "password": ""
  },
  "http_static": {
    "username": "",
    "password": ""
  },
  "ssl": true,
  "certfile": "scribbedsitename.com.cert",
  "keyfile": "scribbedsitename.com.key",
  "require_ssl": true,
  "system_packages": [],
  "npm_packages": [],
  "init_commands": []
}

Added trace log, but there’s no new information. There is an odd object showing up, not decoded:

1 Jan 09:29:51 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 09:29:51 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 09:29:51 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
1 Jan 09:29:51 - [debug] [server:Home Assistant] config server event listener connected
1 Jan 09:33:06 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
1 Jan 09:33:06 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 09:33:06 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 09:33:11 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 09:33:11 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 09:33:11 - [debug] [server:Home Assistant] [object Object]
1 Jan 09:33:11 - [debug] [server:Home Assistant] config server event listener closed

Looks like [object Object] is some kind of permanent failure. It never tries to reconnect after this close. :confused:

It’s possible that it may be a hassIO issue - I’m running NodeRED with websocket connection to HA and have no disconnects while HA is running, however I’m using VENV not HASSIO.

I’m within Docker environments, on an Ubuntu base OS. No hassIO involved. I have discovered the object is a HA Websocket object, not an error object. No idea why reconnect-error is even getting called (added a console.trace into ha-websocket.js):

1 Jan 13:23:10 - [debug] [server:Home Assistant] config server event listener connecting
Trace: o {
  options:
   { setupRetry: 0,
     createSocket: [Function: createSocket],
     self:
      HaWebsocket {
        _events: [Object],
        _eventsCount: 13,
        _maxListeners: 0,
        config: [Object],
        connectionState: 0,
        states: [Object],
        services: [Object],
        statesLoaded: false,
        client: [Circular],
        subscribedEvents: [Set],
        unsubCallback: [Object],
        integrationVersion: 0,
        servicesLoaded: false } },
  commandId: 11,
  commands:
   Map {
     2 => { resolve: [Function],
       reject: [Function],
       callback: [Function],
       subscribe: [Function: subscribe],
       unsubscribe: [Function: unsubscribe] },
     3 => { resolve: [Function],
       reject: [Function],
       callback: [Function],
       subscribe: [Function: subscribe],
       unsubscribe: [Function: unsubscribe] },
     5 => { resolve: [Function],
       reject: [Function],
       callback: [Function],
       subscribe: [Function: subscribe],
       unsubscribe: [Function: unsubscribe] },
     6 => { resolve: [Function],
       reject: [Function],
       callback: [Function],
       subscribe: [Function: subscribe],
       unsubscribe: [Function: unsubscribe] },
     8 => { resolve: [Function],
       reject: [Function],
       callback: [Function],
       subscribe: [Function: subscribe],
       unsubscribe: [Function: unsubscribe] },
     9 => { resolve: [Function],
       reject: [Function],
       callback: [Function],
       subscribe: [Function: subscribe],
       unsubscribe: [Function: unsubscribe] },
     11 => { resolve: [Function],
       reject: [Function],
       callback: [Function],
       subscribe: [Function: subscribe],
       unsubscribe: [Function: unsubscribe] } },
  eventListeners:
   Map {
     'ready' => [ [Function: bound onClientOpen],
       [Function: u],
       [Function: u],
       [Function: u] ],
     'disconnected' => [ [Function: bound onClientClose] ],
     'reconnect-error' => [ [Function: bound onClientError] ] },
  closeRequested: false,
  socket:
   WebSocket {
     _events:
      [Object: null prototype] { message: [Function], close: [Function] },
     _eventsCount: 2,
     _maxListeners: undefined,
     readyState: 3,
     protocol: '',
     _binaryType: 'nodebuffer',
     _closeFrameReceived: true,
     _closeFrameSent: true,
     _closeMessage: '',
     _closeTimer:
      Timeout {
        _called: false,
        _idleTimeout: -1,
        _idlePrev: null,
        _idleNext: null,
        _idleStart: 74563,
        _onTimeout: null,
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(unrefed)]: false,
        [Symbol(asyncId)]: 1783,
        [Symbol(triggerId)]: 1358 },
     _closeCode: 1000,
     _extensions: { 'permessage-deflate': [PerMessageDeflate] },
     _receiver:
      Receiver {
        _writableState: [WritableState],
        writable: false,
        _events: [Object: null prototype] {},
        _eventsCount: 0,
        _maxListeners: undefined,
        _binaryType: 'nodebuffer',
        _extensions: [Object],
        _maxPayload: 104857600,
        _bufferedBytes: 0,
        _buffers: [],
        _compressed: true,
        _payloadLength: 2,
        _mask: undefined,
        _fragmented: 0,
        _masked: false,
        _fin: true,
        _opcode: 8,
        _totalPayloadLength: 0,
        _messageLength: 0,
        _fragments: [],
        _state: 0,
        _loop: false,
        [Symbol(websocket)]: [Circular] },
     _sender:
      Sender {
        _extensions: [Object],
        _socket: [Socket],
        _firstFragment: true,
        _compress: false,
        _bufferedBytes: 0,
        _deflating: false,
        _queue: [] },
     _socket:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: 'hassio',
        _readableState: [ReadableState],
        readable: false,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        parser: null,
        _httpMessage: null,
        timeout: 0,
        write: [Function: writeAfterFIN],
        [Symbol(asyncId)]: 1358,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 40143,
        [Symbol(kBytesWritten)]: 1010,
        [Symbol(websocket)]: undefined },
     _bufferedAmount: 0,
     _isServer: false,
     _redirects: 0,
     url: 'ws://hassio/homeassistant/websocket',
     _req: null },
  _ent:
   { state: [Getter],
     refresh: [Function: c],
     subscribe: [Function: subscribe] },
  _srv:
   { state: [Getter],
     refresh: [Function: c],
     subscribe: [Function: subscribe] },
  _cnf:
   { state: [Getter],
     refresh: [Function: c],
     subscribe: [Function: subscribe] } }
    at HaWebsocket.callback [as onClientError] (/opt/node_modules/node-red-contrib-home-assistant-websocket/lib/ha-websocket.js:266:10)
    at forEach (/opt/node_modules/home-assistant-js-websocket/lib/connection.ts:153:7)
    at Array.forEach (<anonymous>)
    at o.fireEvent (/opt/node_modules/home-assistant-js-websocket/lib/connection.ts:152:48)
    at /opt/node_modules/home-assistant-js-websocket/lib/connection.ts:327:18
    at process._tickCallback (internal/process/next_tick.js:68:7)

According to HA websocket docs, reconnect-error is limited to being triggered only on ERR_INVALID_AUTH and even then, it’s supposed to pass the error code to the callback method. It’s not doing either of those things. :frowning:

ERR_INVALID_AUTH makes no sense since NR does connect for a period of time, then dies. The error is triggered at the time of tick polling, not initial connect: _tickCallback (internal/process/next_tick.js

I’m at a stand-still now. I have no idea what’s going on, and my path for debugging has come to an end. I don’t know how to figure out what home-assistant-js-websocket is doing to throw the error.

Turns out this is an auth error. After a period of time, sockets are closed and reopened. It’s this reopening that’s resulting in an auth_invalid error. No idea why…

I don’t have time at the moment to look at this yet but what happens if you uncheck I use hass.io in the server config of NR and enter the network IP and use a generated token instead of the environment token from hass.io?

Token works but doesn’t work:

Where’s what I found: while tailing the logs, when auth_invalid is shown, I don’t see any new hassio connections within the log. None. It fails, and NR dies.

I validated I do not have any other “hassio” named instances running. This is interesting.

I was trying to determine which access_token you’re using. When using the hass.io addon there are two possibilities for access tokens. You could generate one yourself like I described above or leave the checkbox checked and the HASSIO_TOKEN from the environment variable will be used.

I wanted you to try the other one from which you were using now to see if it still disconnected.

NR doesn’t like the FQDN:

Actually, any URL I put in there gives invalid format for some reason. Since it is in a docker environment, I even tried keeping “hassio” and using my own key - but it kept reverting back to “I use the Hass.io Add-on.”

no path on it. https://example.com:8123

Gotcha, auth makes more sense. I’m actually using a LLAT token and I get no disconnects for NodeRED, but actually I DO get websocket disconnects when I use Tileboard UI + HA’s Auth, without a LLAT token.

I wish I could offer more in a way of a solution but if you can I’d try out a LLAT token if only for testing.

Same error prompt.

Node server is stuck in a loop trying to connect:

“I use the Hass.io Add-On” is the one that actually connects. But it always seems to eventually fail. I even deleted Node Red entirely, rebuilt it from the ground up (only had 4 flows that I previously exported). Same thing, after 15-20 minutes (seems to be on 5 minute socket resets?), connection fails. All using the same auth key from the ENV of the container, HASSIO_TOKEN

Why are you pointing Node Red to an external URL? If you’re using hassio, you don’t have to use your FQDN, it’s using an internal network.

Remove the trailing slash.

I am fully aware of that. My router doesn’t support reverse NAT addressing, but I do have my FQDN setup as a internal mapped host for internal name resolutions, i.e. mysite.com resolves to the internal IP of my hassio box.

I tried the internal IP, the FQDN, the hassio docker host. I’m trying everything. The only thing that actually connects is the hassio alias. But, it doesn’t stay connected.

My SSL certificate is bound to *.mydomain.com, so I tried the FQDN as a precaution that maybe the “Accept Unauthorized SSL Certificates” may not have worked, either.

I’m flipping all the switches - nothing’s working. NR is failing after n number of reconnections (n seems to be ambiguous. I’m not seeing any “x” number patterns).

Ah, it did accept that URL, but still in a loop trying to connect. I used both the FQDN and IP address with “Accept Unauthorized SSL Certificates” checked.

No luck. I even tried insecure, since it’s using ws:// anyway:

The only thing that actually connects is using “I have Hass.io.” It just doesn’t stay connected. It is using the HASSIO_TOKEN env variable. It’s not the token, either - it does connect. But it fails on reconnection:

1 Jan 20:31:03 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:31:03 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:31:03 - [info] Server now running at http://127.0.0.1:46836/
1 Jan 20:31:03 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
1 Jan 20:31:03 - [debug] [server:Home Assistant] config server event listener connected
[20:31:04] INFO: Starting NGinx...

The error you’re getting is being thrown by the hass.io proxy. Check your HA log and see if NR is really connecting on the first two attempts.

Hass.io fakes the connections until it can check-in HA, that’s why there’s a 5-second delay on connection attempts.

Did that. It is. Every successful connection shows a hit within the HA logs, Auth OK:

It’s not until the failed connection that I don’t see a connection attempt within HA.

PS: I have noticed 1 pattern in the socket connections - they appear to disconnect after 5 minutes. It’s pretty consistent.

Is there any socket timeout I can set to make this indefinite, never timing out? Or something much, much longer?

Looks at that, ~ every 5 minutes:

1 Jan 20:33:38 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
1 Jan 20:33:38 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:33:38 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:33:38 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:33:38 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:33:43 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:33:43 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:33:48 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:33:48 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:34:00 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:34:00 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:34:00 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
1 Jan 20:34:00 - [debug] [server:Home Assistant] config server event listener connected
1 Jan 20:38:36 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
1 Jan 20:38:36 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:38:36 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:38:36 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:38:36 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:38:41 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:38:41 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:38:46 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:38:46 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:38:46 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
1 Jan 20:38:46 - [debug] [server:Home Assistant] config server event listener connected
1 Jan 20:43:40 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
1 Jan 20:43:40 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:43:40 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:43:40 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:43:40 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:43:45 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:43:45 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:43:50 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:43:50 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:43:55 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:43:55 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:43:55 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
1 Jan 20:43:55 - [debug] [server:Home Assistant] config server event listener connected
1 Jan 20:48:40 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
1 Jan 20:48:40 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:48:40 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:48:40 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:48:40 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:48:45 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:48:45 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:48:50 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:48:50 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:48:55 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:48:55 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:48:55 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
1 Jan 20:48:55 - [debug] [server:Home Assistant] config server event listener connected
1 Jan 20:53:38 - [info] [server:Home Assistant] WebSocket Closed http://hassio/homeassistant
1 Jan 20:53:38 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:53:38 - [debug] [server:Home Assistant] config server event listener closed
1 Jan 20:53:39 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:53:39 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:53:44 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:53:44 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:53:49 - [debug] [server:Home Assistant] WebSocket Connecting http://hassio/homeassistant
1 Jan 20:53:49 - [debug] [server:Home Assistant] config server event listener connecting
1 Jan 20:53:49 - [info] [server:Home Assistant] WebSocket Connected to http://hassio/homeassistant
1 Jan 20:53:49 - [debug] [server:Home Assistant] config server event listener connected