Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I will explain the context of the problem first. For a University project I'm going to implement an Android application. The Android application (and others) will be able to send data to a remote source, that remote source will also be able to send messages to the Android applications. For example the messages may be "apply configuration x", "what is your status". I want the remote source to be able to send these messages to:

  1. Reduce overhead. I don't want the Android devices sending data when its not necessary. This uses data and power.
  2. So that I can scale it, for example version 1 might allow you to check what value x is on the Android applications, version 2 might let you check what y is etc.

I have looked at implementing this using Sockets in Java. Having the Android application talk to the remote source is easy. I configure the router (port forward) and everything is OK. The problem is having the remote source to talk to the Android application. They may be using public WiFi hotspots, have their 3G on etc, I wouldn't be able to address the device directly via sockets right?

The approach to solving the problem is not that common I believe, which makes me believe I'm trying to do something that shouldn't be done. Can anyone shed some light on this and offer me some guidance?

Many thanks.

share|improve this question
1  
You might investigate using a message queue as the hub for communication. The realtime-ness of this has to do with how often the queue is polled. –  MichaelT Jan 16 '13 at 22:35
    
Will this allow android app to send a message to the server, and then server to send one to android app? The one sent from the server not being a response but an actual message. –  Thomas Hutchinson Jan 16 '13 at 22:39
    
A message queue is an application running somewhere that acts as a 'post office' of sorts for applications connecting to it. It makes some guarantees about the delivery of the message. If something is a message or a response to a message is semantics of the message headers. –  MichaelT Jan 16 '13 at 22:47
add comment

2 Answers 2

As you say, typically it is very difficult or impossible to establish a socket connection originating at a central server and attempting to connect to mobile devices. Instead, what you can do is originate the connection at the mobile device, connect to the central server, and leave the socket open waiting for a command from the central server. Of course the first thing your Android code would do on connection is identify itself to the central server, so the central server knows who it is talking to.

Fortunately, Google has already done this for you in Google Cloud Messaging for Android:

Google Cloud Messaging for Android (GCM) is a service that allows you to send data from your server to your users' Android-powered device. This could be a lightweight message telling your app there is new data to be fetched from the server (for instance, a movie uploaded by a friend), or it could be a message containing up to 4kb of payload data (so apps like instant messaging can consume the message directly).

share|improve this answer
    
Thanks a lot Greg! This helps me a lot. I'll read up on GCM. From experience do you know if there would be much overhead from keeping the socket connection open? The project is still new but from assumption I would say that the application should be expected to run for 7 hours on the android and messages sent from the server will be small Java Objects and sent very infrequent e.g. 1 per every 2 hours. Sorry if I'm vague, there is no hard numbers. –  Thomas Hutchinson Jan 16 '13 at 22:49
    
If you have a limited number of client devices (say, less than 10,000) then it's quite reasonable to have them connect directly to your server and wait for messages. If you have more devices than that, you'll probably need more servers. You may have to tweak OS settings on your server to let it have 10,000 open socket connections at once. –  Greg Hewgill Jan 17 '13 at 23:16
add comment

I think you're confusing connection with communication. Your application can open communications with the server using TCP or UDP and just sit there without sending any data. A bidirectional pipe is open, and can be used to send messages initiated at either end of the connection.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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