Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Sockets vs Polling

As part of an Android app I'm developing there is a chat room feature. We have a server which can process the incoming messages and store the messages. Is it better to keep a socket connection open between the phone and the server so the server can send any new messages to the phone, or is it better for the phone to poll the server for new chat messages?

like image 608
Jonny Avatar asked Sep 15 '11 15:09

Jonny


People also ask

Is WebSocket better than polling?

WebSockets keeps a unique connection open while eliminating the latency problems that arise with long polling. Full-duplex asynchronous messaging is supported so that both the client and the server can stream messages to each other independently.

Does Socket.IO use polling?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

Is Socket.IO long polling?

First, Socket.IO creates a long-polling connection using xhr-polling. Then, once this is established, it upgrades to the best connection method available.

Can we use WebSockets in Android?

The WebSocket protocol paved the way to a truly realtime web. At the time of writing this article, the Android SDK does not have native support for WebSockets. However, it relies on the Java Development Kit (JDK), which includes support for WebSockets as part of javax. websocket package.


2 Answers

It is bad solution with poll for app that have randomly posting data. What I want to say is that polling data is useful when you have something that is happening discrete like every 5 minutes or something like that. this is not the case with chat, some user can post something ones in a hour , some can post 30 times in 2 minutes

so keep your sockets open

like image 169
Lukap Avatar answered Sep 27 '22 18:09

Lukap


Polling lacks real-time connection and a persistant connection is battery draining. I think that what you are looking for is a combination of "push"-ing and persistant connection. You would wake your phone via push, and then establish a connection via sockets to handle chat.

I suggest reading this article. I'm not sure if it mentions c2dm, the google push service.

like image 45
ggpuz Avatar answered Sep 27 '22 18:09

ggpuz