Search code examples
androidcscalabilityhole-punchingnat-traversal

Working behind the NATs - a scheme for device communication


I'm trying to come up with a solution enabling data exchange between an embedded device (xMega128(C) based) and an Android apps. The catch is the data exchange must be conducted via the Internet and both the embedded device and the mobile device running the app can be behind different NATs, connecting using different ISPs, 3G, LTE, etc.

I tried UDP hole punching, but it does not work with symmetric NATs. Multi hole punching with prediction also does not guarantee 100% reliabilty. I also considered using ICE, but ICE C libraries (pjnath, libnice) are incompatible with the hardware chosen (libs require os). Right now I'm considering implementing or use (if exists) traffic relay server, but that just seems like a hack to me.

Are there any other options I hadn't considered? Any help will be appreciated.

Ideally, the communication scheme would be:

  • 100% reliable

  • relatively low-latency (3 seconds absolute max)

  • scalable (say up to 500k devices in the future)

  • initializable by both the app and the device

  • multi-user – one device would connect to many android apps

Also, if this helps, the data exchange between the device and the app is not very high-intensity – roughly 1 session per hour, ~50 messages per session with 10-20 seconds between them, each message weighing around 100 bytes.


Solution

  • What you're describing is effectively peer to peer or a subset thereof and to get that working reliably is a lot of work. Where peer to peer fails you normally fall back to a relay server. It can be done but the amount work to do it is quite large. Your list of requirements is also quite steep...

    100% reliable

    There's no such thing as a reliable connection. You need to build in fault tolerance to the app to make it reliable.

    relatively low-latency (3 seconds absolute max)

    Quite often you will be limited by physics ie speed of light. Low latency is hard.

    scalable (say up to 500k devices in the future)

    I don't know what this means ie is this concurrent connections?

    From wikipedia on NAT Traversal

    Many techniques exist, but no single method works in every situation since NAT behavior is not standardized. Many NAT traversal techniques require assistance from a server at a publicly routable IP address. Some methods use the server only when establishing the connection, while others are based on relaying all data through it, which adds bandwidth costs and increases latency, detrimental to real-time voice and video communications.

    ie it will work sometimes ie it will be unreliable so you need to user several methods to make it reliable.