What is the best way to get data from server as fast as it gets to it? (Like instant message, or simple broadcast for example).
I first thought about using miniature web server in android and get post requests to it with data. However with this approach I soon understood that I will get many problems with NAT and similar issues.
Later I thought about manually pooling the server from android, but it seems somewhat flaky approach to problem, because data is got in irregular intervals (like image with some text).
I was thinking about using something like webrtc or rtp to have connection between server, but these approaches are somewhat complex and half baked. I found very little info on those about data operation. Thought I managed to get in my „experiments“ video and sound transfer, I found very little info how to get data transfers.
What should I do? Now I am thinking about having some connection to server and getting some events in android, which when I got update in server could download everything from server.
Update:
My data is encode64 processed html5 canvas png image. Probably I will add to it some text data to blob. It is generated on webpage it should reach phone as fast as it can through server. The problem is part where image data goes from server to phone. Data size per request is ~50KB.
8
I’m not quite sure about the scenario you are trying to describe but would a pub sub model help? Publish messages to a topic and have anyone who is interested in that information subscribe to the same topic. MQTT is a light weight pub / sub messaging protocol that only has 2 bytes of header and so is quick. There are several clients and servers available, some that are open source or free to use e.g. Eclipse Paho client and the Mosquito server.
2
Depending on the device you’re using, you’re going to struggle. Android has a couple of problems in the design of its networking subsystem that are going to cause you grief.
The primary issue is power management. Android devices are, as a rule, mobile devices running on battery power for the majority of the time. Therefore, they conserve power as much as possible, aggressively putting the cpu to sleep whenever it hasn’t been locked in an awake state. In many android devices, this causes the device to stop receiving packets on any wireless network it may be connected to. This makes a push protocol over wireless next to useless: in order for it to work you have to keep the cpu awake, which will kill the battery pretty quickly.
You may think you can solve this by not using the wireless network but connecting to your server over 3g, but this doesn’t work. Android automatically prefers wireless over 3g when it is connected, and the only api that lets you use 3g by preference is only usable for short duration connections because it has a timeout that is limited to a short period (30s, iirc).
Now, if you can restrict your target device to one that supports wake-on-wlan, maybe you can make it work, but you’ll find it tricky.