Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM vs. Socket in android

In my knowledge, most of android tutorials and examples out there rely on use of GCM for sending data from server to android device; and use php scripts along with post/get methods to send data from device to server.

A friend of mine(someone who doesn't have any knowledge of android programming) simply asked me, why can't we use Socket class in java? In traditional java programming, you use sockets (IP adress + port no.) to achieve functionality similar to GCM (single server multiple clients - using proper techniques you can ensure messages are not lost, just like gcm).

Can this traditional socket approach be implemented in android as well? If so, what are the pros and cons of GCM and Sockets? In which situations would the use of sockets be appropriate? Whatsapp and lot of other apps, to my knowledge, use GCM. Are there apps which use sockets?

P.S: I'm just a C.S. student; apologies if I've got my concepts wrong somewhere

like image 368
MMss Avatar asked Oct 02 '13 04:10

MMss


1 Answers

GCM uses sockets too. However, they are managed by the system and thus can do things a regular app cannot do. Generally, for any type of 'push' notifications, the system needs to have a long-lived socket, and a 'notification' is just some data received on that socket. All of this is not too hard to implement on an always connected machine, such as a desktop or a server, but there are additional challenges on mobile. For example, switching networks (3G<->WiFi), going out of range, maintaining a connection without keeping the device awake all the time (and killing the battery in the process). So yes, you could implement something similar using a service , sockets and your own server, but it's not trivial on mobile.

Check out http://mqtt.org/ for an open-source implementation.

like image 177
Nikolay Elenkov Avatar answered Oct 20 '22 06:10

Nikolay Elenkov