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
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.
Related
I created a little game and now I am trying to make a multiplayer game out of it. One android phone should host the game and the other phone should be able to connect to this server. My question is how can I tell the Client to which IP he should connect. Is there a way to scan for hosts?
For Android games there is a perfect (and simple) library called kryonet.
It has a method - discoverHost - for scanning local network for devices that are listening for incoming connections.
Also, it contains classes such as:
Client
Server
that makes implementing simple communication protocol (on both UDP and TCP protocols) really easy.
As many people uses this library (including me) - you can find a lot of examples:
The Simplest and Easiest Game Network layer with KRYONET
KryoNet Client and Server Example
Kryonet's itself examples
... and many others. :)
Update 1:
There is also a great library from Google called Nearby Connections API that allows to autodiscover devices in local network and allows to create multiplayer (or multiscreen) games.
Furthermore, Google has created nice (multiplatform) APIs for multiplayer games over the WAN:
Real-time Multiplayer API
Turn-based Multiplayer API.
Sample for Nearby Connections and Real-time Multiplayer APIs you can find here.
You can use Network Service Discovery:
http://developer.android.com/intl/es/training/connect-devices-wirelessly/nsd.html
If I understand you correctly, you want the game host to advertise itself in the reachable network to clients to join.
There are many possibilities around this on different levels of the network protocol stack.
Somethings to look at, that come to my mind:
Rendezvous protocols: https://en.wikipedia.org/wiki/Rendezvous_protocol
Bonjour: https://de.wikipedia.org/wiki/Bonjour_%28Apple%29
A well known central directory server, that keeps track of who is offering and who wants to play (LDAP, DNS, ...)
Word-of-Mouth: Tell your IP to the user and she will broadcast it via voice to other players.
IRC channels or some other means of central communication established outside of your control, like a Twitter account.
You see, there are quite some possibilities. Maybe you want to narrow down you question to your specific needs:
Who is the audience, is it a "LAN-party", other phones somewhere in the mobile space?
How much effort do you want to put into it?
What platforms do you need to support?
I'm developing an Android app that requires 2 (or more) devices to communicate with each other.
I tried using Google Cloud Messaging but I was disappointed to find out that the GCM max capacity is 100 messages, so it is broken and does not fit my requirements.
I was thinking about java sockets. Every device will open a new socket (or keep its socket open) and communicate with a group of sockets (devices).
In order to communicate this way I need a server-side app that can send messages to the client (android device). So I figured out that HTTP or web-service won't help me. Am I right?
What is the best way for me to implement such a server-side app?
You can refer to this question I previously asked and implemented. It was for implementing my own Notification mechanism but it equally (or even more) applies to chatting applications since message queues perfectly fit that usecase.
Building an Android notification server
I ended up not doing it and using GCM at the end but I did have a fully working solution using ActiveMQ and Paho. You can research them both and understand their inner workings. It's easy in principle and definitely possible but the problem is, you may not be able to do this for iOS or WP as it requires running a service in the background (in case your app is not open and you want to make sure the messages are at least sent in a notification).
The possible solution to that problem would be to use both the notification service (GCM or equivalent) for background notifications and then using your MQ for actual communication but I decided that was too much for my project.
If you look at Paho, it will have a fully working MQTT solution that will work even if the phone is not "online" (sleeping or otherwise) and there are plenty of samples for ActiveMQ and drivers for multiple programming languages.
I think this solution is much better than having open sockets between two apps, at least because they allow you to persist messages and guarantee delivery which is an important aspect for a chatting application.
As it is said by kha, choose one of the message queue protocols is the best solution. 3 reasons in brief,
Delivery guaranteed regardless of temporary offline or long latency.
As simple as subscribe / publish, no worry about transport layer any more.
Broker available online. You save time and money for setting up your own.
For mobile devices like in your case, I'd prioritize MQTT too. It's lightweight and stable. If you are totally new to message queue or MQTT, refer to this documentaion and example code
I've had a look around and could not find what I'm looking for, so please feel free to redirect me.
I'm writing a card game that uses a Java server that stores the card information. I want to develop an Android app that connects to this server (via TCP/IP is my initial thought) and issues the commands i.e. pick up cards, play cards etc. (as the cards are stored in arraylists within the server app.)
My knowledge of Android is extremely limited and I cant find any good sources that could possibly help me, or explain how android networking works.
You need tcp server socket communication >
http://developer.android.com/reference/java/net/Socket.html
I would recommend to use a REST service and communicate via JSON. You can use for example Robospice in combination with Jackson2 which also brings you async communication and caching out of the box.
https://github.com/stephanenicolas/robospice/wiki/Starter-guide
Socket communication is really old school ;-)
I'm writing a bingo application for android.
bingo game has alerts and notifications that supposed to go from the server to the client on real time.
the client also needs to notify the server in real time whenever he yells bingo or any other events.
now.. my question is, what's the recommended method for two ways communication between server and android client?
should I use websockets? regular sockets? long polling ? anything else ?
if I use sockets as the method of choice, what happens when the cellular phone is changing wan networks ? do i need to detect such a thing and reconnect the socket?
any information regarding the issue would be greatly appreciated.
The easiest method is to use Android GCM for server to client communication, and a RESTful API for client to server communication.
You can even try Parse library, that comes with their own SDK and solves most of the configuration.
I want to develop an chat application to chat between two or more android devices using wifi network.The application should be able to send or receive strings from each other. I have an experience of using Bluetooth network between pc and android. Can any give me any suggestion or right direction. Thanks in advance.
You can be able to chat between two android devices in Android Version 4.0.There is an API called 'Wifi P2P'.More information can be had from here:
http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html
If you are thinking about connecting devices that are under the same WiFi AP/router and without setting up any server, then I would suggest you to consider using UDP multicast which has been available since API level 1:
http://developer.android.com/reference/java/net/MulticastSocket.html
UDP does not guarantee data delivery (could be lost), so I would use UDP multicast for device discovery, and open up a TCP connection for data that requires guaranteed delivery. TCP listening port can be advertised via UDP multicast so that everyone can connect with each other over TCP. (There may be 3rd party tool that does this low level stuff for you.)
maybe XMPP will help, google talk uses the same protocol.
The Extensible Messaging and Presence Protocol (XMPP) is an open
technology for real-time communication, which powers a wide range of
applications including instant messaging, presence, multi-party chat,
voice and video calls, collaboration, lightweight middleware, content
syndication, and generalized routing of XML data.
things you need to do:
1. you need a server computer to run XMPP server, there are lots of open source implementations.
2. you need a XMPP client running on your android device, there are also lots of open source implementations running on android platform.