Anyway to know if a websocket is closed?

MQTT has a nice .isConnected(), but I don't see anything similar for WebSockets. Is there? The problem is, when I call interfaces.webSocket.sendMessage even if the socket is closed, it seems to succeed (no errors) -- so if I've been disconnected, how do I know so I can reconnect?

I don't think there is any way to know outside of doing a periodic heartbeat from the data source. I believe the Hubitat websocket sends out a heartbeat every 30s, for instance.

If the end websocket you are connecting to doesn't do that, then I don't know of any way to know. I'm sure someone will post if there is a way, and then I'll learn something new. :slight_smile:

Based on below I guess I was wrong. Not the 1st time that has happened. :slight_smile:

You do get a message in the status handler, so I guess I can set a state variable when I get that or something...

My Logitech Harmony Hub driver implements a connection to a webSocket and automatically reconnects if the connection is lost. You could take a look at it for ideas. It's been a long time since I wrote that code, though... :thinking: I can't recally exactly how it works... I do recall a ping/pong heartbeat...

I actually took a look at your code but it looks like you're using the deprecated WebSocket API, not the new one, so I didn't want to build something new using that.

1 Like

Take a look at my Log Watchdog driver, I think it'll have what you're looking for.

2 Likes

Probably! I haven't tweaked that portion of the code in a very long time.

"If it isn't broken, don't fix it!" :wink:

1 Like

Hmm, thanks. That's close to what I'm looking for. First just curious, how did you figure it out? I've yet to see any status messages with "failure:"?

For me, I'm getting the "status: closing" because I think the server is gracefully disconnecting me. That's when I need to do my reconnect. However, when I did it in the status handler I got in a weird loop where it would be connecting and disconnecting over and over. What I instead did was pulled the reconnect code out and put it in my ping routine. This seems to work.

But now, based on your code I need to add support for handling failure: messages :slight_smile:

This is what I have so far hubitat-orbitbhyve/orbit-bhyve-bridge.groovy at master · dcmeglio/hubitat-orbitbhyve · GitHub -- note for anyone with the Orbit Bhyve looking at this, this code is mostly working for me, but I don't know if it's really ready for primetime yet.

1 Like

Check out this post for more information...

1 Like

Unfortunately that all looks like it’s for the deprecated version?

If you are running MQTT on another computer couldn't you run a curl command to ping the api on the socket from that machine and see what the response is (or heaven forbid telnet)? Based on the response could use the Hubitat maker API or MQTT to throw a virtual switch.

Sorry, not sure I follow. How would that help me with a websocket? MQTT isn't involved?

What’s weird to me is if I send to a websocket that’s closed I get no errors???