NRCHKB / node-red-contrib-unifi-os

Nodes to access UniFi data using endpoints and websockets
Apache License 2.0
28 stars 1 forks source link

Websocket yields error 1006 repeatedly #28

Closed franklinvv closed 3 years ago

franklinvv commented 3 years ago

I have a websocket node that connects to my UDM Pro to the /proxy/protect/ws/updates endpoint, and it receives data successfully, but every so often it disconnects with an error 1006 after which it reconnects after a couple of seconds. In the logs, I can see this:

09 Sept 08:29:41 UniFi:AccessController [X:fed2343.943a0c8] Returning stored auth cookie +30s                                                                          
09 Sept 08:29:41 UniFi:WebSocket [WebSocket:d63cf345.9f1cb8] Connection to wss://<IP> open +30s                                                                           
09 Sept 08:29:41 UniFi:WebSocket [WebSocket:d63cf345.9f1cb8] Connection to wss://<IP> closed. Code:1006 +4ms                                                              
09 Sept 08:29:41 UniFi-Error:WebSocket [WebSocket:d63cf345.9f1cb8] Is UniFi server down? +30s                                                                                 
09 Sept 08:30:11 UniFi:AccessController [X:fed2343.943a0c8] Returning stored auth cookie +30s                                                                          
09 Sept 08:30:11 UniFi:WebSocket [WebSocket:d63cf345.9f1cb8] Connection to wss://<IP> open +30s                                                                           
09 Sept 08:30:11 UniFi:WebSocket [WebSocket:d63cf345.9f1cb8] Connection to wss://<IP> closed. Code:1006 +4ms                                                              
09 Sept 08:30:11 UniFi-Error:WebSocket [WebSocket:d63cf345.9f1cb8] Is UniFi server down? +30s                                                                                 
09 Sept 08:30:41 UniFi:AccessController [X:fed2343.943a0c8] Returning stored auth cookie +30s                                                                          
09 Sept 08:30:41 UniFi:WebSocket [WebSocket:d63cf345.9f1cb8] Connection to wss://<IP> open +30s                                                                           
09 Sept 08:30:41 UniFi:WebSocket [WebSocket:d63cf345.9f1cb8] Connection to wss://<IP> closed. Code:1006 +3ms                                                              
09 Sept 08:30:41 UniFi-Error:WebSocket [WebSocket:d63cf345.9f1cb8] Is UniFi server down? +30s     

The error repeats every 30 seconds, which correlates with the reconnect timeout of 30000ms set in the node.

I am using version 0.5.0 of this project in Node-RED v2.0.6.

crxporter commented 3 years ago

The /proxy/protect/ws/updates requires a query string.

The query string comes from an http call to /proxy/protect/api/bootstrap

The way I'm doing it is like this:

Start with an http node pointing to /proxy/protect/api/bootstrap

Run the output of that to this function node:

if ("lastUpdateId" in msg.payload) {
    return {
        payload: {
            endpoint: `/proxy/protect/ws/updates?lastUpdateId=${msg.payload.lastUpdateId}`
        }
    };
}

Finally connect the output of that function node to the input of a websocket node with this endpoint configured: /proxy/protect/ws/updates?

To speed up your config, feel free to have a look at my flow:

[{"id":"ed307f26.4c36e8","type":"inject","z":"93dc4db2.de8ac","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":420,"wires":[["5e958d35.6f65cc"]]},{"id":"5e958d35.6f65cc","type":"unifi-request","z":"93dc4db2.de8ac","name":"","accessControllerNodeId":"bb62493.c90b9b8","endpoint":"/proxy/protect/api/bootstrap","method":"GET","data":"{}","x":290,"y":420,"wires":[["1b0f15fa.bf17ea","69ca3313.d939ec","25d193e8.931aa4"]]},{"id":"25d193e8.931aa4","type":"function","z":"93dc4db2.de8ac","name":"lastUpdateId","func":"if (\"lastUpdateId\" in msg.payload) {\n    return {\n        payload: {\n            endpoint: `/proxy/protect/ws/updates?lastUpdateId=${msg.payload.lastUpdateId}`\n        }\n    };\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":420,"wires":[["367d1b38.56ec24","1b0f15fa.bf17ea"]]},{"id":"367d1b38.56ec24","type":"unifi-web-socket","z":"93dc4db2.de8ac","name":"Protect","endpoint":"/proxy/protect/ws/updates?","accessControllerNodeId":"bb62493.c90b9b8","reconnectTimeout":30000,"x":680,"y":420,"wires":[["d9602475e9621fc9","17a5572.c9bdea9","4668a261.89730c","f0be6569.37883"],["d553721c.049278","02ad55e4c08f4a42"]]},{"id":"bb62493.c90b9b8","type":"unifi-access-controller","name":"Bloken","controllerIp":"10.0.0.1"}]

I've been getting push updates from this endpoint for months now. Actually I've sorted it enough that I'm watching smart detect events being pushed to nodered from the websocket! Have a look at my kids headed to school this morning (most recent smart detect)

Screen Shot 2021-09-09 at 7 29 47 AM

franklinvv commented 3 years ago

Great to see that it should be working. 🙂 Actually, I do have a flow to get the last update ID from the bootstrap endpoint, which appears to be working, set up in the way described in the README. However, it was unclear to me what I should be putting in the endpoint field for the websocket node. I figured it should be empty because the function feeds an endpoint into the node, but you have /proxy/protect/ws/updates? as an endpoint. Does that make a difference?

Also, I had to restart Node-RED to get rid of the 1006 errors, because multiple instances of the websocket node seemed to be lingering in the background, hammering my logs with this error.

crxporter commented 3 years ago

but you have /proxy/protect/ws/updates? as an endpoint. Does that make a difference?

I don't actually know if it makes any difference... I know this works for me though! I have a couple other endpoints as well.

To check, are you on UDM-Pro? Restarting nodered after a bit of struggle to set up is a great idea. Some connections might get "lost" ... If you still see the 1006 you may need to delete all your unifi nodes, restart, then add fresh with the corrected setups.

crxporter commented 3 years ago

PS: if you learn some things and want to make a PR to add information to the readme, please do! We are still very early production in this node-red-contrib.

franklinvv commented 3 years ago

PS: if you learn some things and want to make a PR to add information to the readme, please do! We are still very early production in this node-red-contrib.

I would, but my JS skills are virtually non-existent. ;) I mainly do Rust, Kotlin, Java, Obj-C and Swift.

To check, are you on UDM-Pro?

I am! Everything seems to be working properly now, although I can't for the life of me figure out what endpoint you retrieve that smart detect image from? The only protect endpoints that I can find are bootstrap, cameras and updates?

crxporter commented 3 years ago

Started discussion #30 for the endpoints and smart detect chat.