Skip to main content
Aug 25, 2015 3:43 AM UTC
Question

Websocket Disconnects

Re-posting Mike Mahoney's blog post:

"Websockets disconnects can happen by choice, or due to exceptional/error conditions.  Here is some information about what happens in each case:

  • Clean disconnect:  During a clean disconnect, the user or application will initiate a disconnect sequence.  This has two steps.  First, if one or more things are bound to the websocket, the client will send an UNBIND message to the PLATFORM.  This will cause Things bound to the websocket to appear as offline (their isConnected property will be set to false).  Then, the client will close the websocket according to the websocket protocol.
  • Exceptions/Errors:  In many cases, the underlying network interface will be aware of unexpected disconnects.  For example, if you forcefully kill an EMS, the Platform's network interface will be notified, the websocket will be closed, and Things will be cleanly unbound.  In those cases, the updating of the isConnected property will occur very quickly.

    In some cases, however, the client or the server may not be aware that the network connection has become stale.  For example, if a network cable is unplugged, a network interface is shut down, or an intermediate component in the connection (such as a proxy server) becomes disconnected.  In those cases the Platform will not become aware of the device's disconnected state until a timeout occurs.

    By default, the EMS and SDKs periodically send a websocket ping to the Platform, and the Platform responds with a pong.  The Platform tracks when the last ping was received for each client.  By default, if a ping hasn't been received in 60 seconds, and the connection has been otherwise inactive, the Platform will close the websocket.  This timeout is configurable in the WSCommunicationSubsystem configuration, "Idle Connection Timeout (sec)."

    Similarly, if the EMS does not receive a pong within a certain amount of time after sending a ping, it will close the websocket and begin attempting to reconnect.  For the EMS, this ping/pong timeout can be set using ws_connection.pingpong_timeout and is set in milliseconds."