QBoard » Statistical modeling » Stats - Tech » Is this an appropriate use of websocket programming?

Is this an appropriate use of websocket programming?

  • I am incredibly new to the topic of websockets and am trying to figure out the correct way to handle the communication between a device and a server.

    Here is my scenario: I have a thermostat (very similar to the Nest) that needs to communicate with a web server. Every time you change the temperature on the thermostat, I need to send data to the web server to update it's "current stats" in the database. Easy, I can do that.

    The part that I am confused about, and think websockets might be a use-case is when the user changes the temperature from the web interface. The thermostat has to pull in that information from the server to see "Oh, okay you want it to be 66 degrees."

    I've thought of having the thermostat long-polling the server every 2-5 seconds to see what the "current stats" are in the database to change the temperature, but that seems like overkill.

    Is there a way to open a connection between the thermostat and the server to listen for messages?

    I began down the road of reading about websockets, however, I believe it is unfortunately browser-based only.

    As I'm fairly new to the game with regards to these types of connections, if anyone could point me in the right direction regarding protocols, communication, etc. I would greatly appreciate it!

    Tech Specs

    Server is written in Ruby on Rails

    Thermostat is written in Java

      August 25, 2021 3:03 PM IST
    0
  • Websockets can be used between any two programs which need to communicate, they are certainly not restricted to the browser. That said, should you be using websockets is a different question. one thing to think about is that websockets involves a persistent connection. this may not scale (if you have lots of devices) and it may also be overkill. if you are expecting the temperature to be changed once a day, having a persistent connection for the entire day is an enormous waste of resources. websockets are typically used when communication needs to be "fast" and relatively frequent. unless you really need instantaneous updates in the thermostat, i would just have it ping the server every few minutes for updates.

    Side note, websockets is fairly new, so any libraries you end up using may be a bit on the immature side.

    We prototyped some java to java websockets a while not too long ago. We used the ning async library on the client side and the atmosphere library (built on netty) on the server side.

      August 27, 2021 1:10 PM IST
    0
  • The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.


    ebSocket

    The primary interface for connecting to a WebSocket server and then sending and receiving data on the connection.

    CloseEvent

    The event sent by the WebSocket object when the connection closes.

    MessageEvent

    The event sent by the WebSocket object when a message is received from the server.

      January 13, 2022 1:43 PM IST
    0
  • WebSockets is just a specification for tunneling something similar to TCP sockets over HTTP; it's not confined to the browser, and client libraries are available for most common languages.

    This sounds like a reasonable use case for a long-running connection, but I would generally prefer a raw TCP connection to a WebSockets connection unless you have a specific restriction in mind (e.g., most home Internet connections have no problem with connecting to a server at an arbitrary port).

      August 31, 2021 12:38 PM IST
    0