7

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)

anonymous2
  • 4,872
  • 3
  • 20
  • 48
bermick
  • 81
  • 5

1 Answers1

3

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.

hardillb
  • 12,553
  • 1
  • 20
  • 34
  • thanks for your answer @hardillb. That TCP connection stays open forever? is it a socket? – bermick Mar 02 '18 at 11:11
  • Yes, it effectively stays open forever – hardillb Mar 02 '18 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 06 '18 at 09:15
  • What existing solutions implement it, instead of writing a server that holds many tcp connections ourselves – Nathan B Jul 23 '20 at 11:31
  • @NadavB e.g. MQTT (nearly all the large cloud providers IoT offerings use it) – hardillb Jul 23 '20 at 11:35