I am implementing a light controlling with MQTT/node which consists of some elements mainly these: device (behind a NAT), server (mqtt/broker), client (web browser)

Part of the architecture/process I came up with was: The device needs to open a socket with the server and keep it open (and not the other way because of NAT), so whenever the client sends a control command to the server, the server sends it to the device via the opened websocket.

So my broader question would be: How are device-behind-NAT/server connections handled normally in IoT?

NOTE: I’ve seen lots of questions explaining the case when a device writes to the cloud and then a client reads from it, which doesn’t need the socket open all the time (just when the device writes) But haven’t seen the case when the server/client want to write to the device (from outside the NAT)

  • Welcome to IoT.SE! I would recommend narrowing this down just a hair more. Furthermore, your title doesn't match the question posed at the end of your question. It would be great if you could make this more cohesive so that the question follows a single problem or question that you want answered.
    – anonymous2
    Mar 1, 2018 at 23:15
  • Also a Philips Hue bulb doesn't connect directly to the cloud, it uses a gateway. The gateway maintains a outbound connection to the cloud
    – hardillb
    Mar 2, 2018 at 7:34
  • Your title has many existing answers, in this forum, on stackoverflow.com and on softwareengineering.stackexchange.com I am not so sure about your actual question, though
    – Mawg
    Mar 2, 2018 at 8:34
  • thanks @anonymous2 I will narrow it better, though the last question do matches the title question. Mawg I’ve seen a lot of questions/articles explaining an architecture where the client only reads content, for example a sensor temperature opening the socket to the cloud and writing to it, so the client can read from it later on. I Haven’t seen this case (which requires a different approach to be able to write FROM the cloud TO the device behind nat)
    – bermick
    Mar 2, 2018 at 8:42

1 Answer 1


They are handled by the device connecting out and maintaining an on going TCP connection.

TCP connections are bi-directional once opened so as long as the device opens the connection outbound through the NAT gateway the cloud can push information/commands back down that link.

  • thanks for your answer @hardillb. That TCP connection stays open forever? is it a socket?
    – bermick
    Mar 2, 2018 at 11:11
  • Yes, it effectively stays open forever
    – hardillb
    Mar 2, 2018 at 11:12
  • Simplifying it, it stays open forever. In the reality, there has to be some pinging between client and broker, so that the connection stays alive, and also you have to bear in mind reconnection strategies, for when the connection actually gets broken.
    – bgusach
    Mar 6, 2018 at 9:15
  • What existing solutions implement it, instead of writing a server that holds many tcp connections ourselves
    – Nathan G
    Jul 23, 2020 at 11:31
  • @NadavB e.g. MQTT (nearly all the large cloud providers IoT offerings use it)
    – hardillb
    Jul 23, 2020 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.