LearnBoost/socket.io — Issue #651: Android XHR longpoll "reconnects" each time
Author: Juriy
Created: 2011-11-19 13:03:06+00:00
State: open

Hello guys, I've seen that there are couple more topics on the subject, but I still couldn't find the andswer to the question. So here's the problem: Android continiously reconnects to the server as the new client.

Little bit more details on the subject.

### Client
* Device: Samsung Galaxy (GT-I9000)
* Android version: 2.2
* Build number: FROYO.XWJP6

### Server
* Node.js version: v0.6.1 running on Windows 7
* Socket IO version: 0.8.7

### List of installed modules (npm ls)
```
+-- async@0.1.15
+-- ejs@0.4.3
+-T express@2.5.0
¦ +-- connect@1.7.3
¦ +-- mime@1.2.4
¦ +-- mkdirp@0.0.7
¦ L-- qs@0.3.2
L-T socket.io@0.8.7
+-- policyfile@0.0.4
+-- redis@0.6.7
L-T socket.io-client@0.8.7
+-- uglify-js@1.0.6
+-- websocket-client@1.0.0
L-- xmlhttprequest@1.2.2
```

### The server code:

```javascript
var express = require("express");

var app = express.createServer();
var io = require('socket.io').listen(app);

app.use(app.router);
app.use(express.static(__dirname + "/public"));

app.get("/", function(req, res) {
res.sendfile(__dirname + "/public/dbg.html");
});

var maxUserId = 0;

// This limit is here to check if desktop browser behaves the same
// way. No it is not.
io.set('transports', ['xhr-polling']);
io.sockets.on('connection', function (socket) {
var userId = maxUserId++;
var userName = "User " + userId;

socket.emit('message', { text: "You have connected" });

socket.broadcast.emit('user-joined', {name: userName});

socket.on('disconnect', function () {
socket.broadcast.emit('user-left', {name: userName});
});
});

app.listen(80);
```

### The client code
dbg.html that is served as static from the server

```html

```

### Observed behaviour:
When I connect to the server with the desktop browser, everything is running fine, I'm getting single "You have connected" message to the console, no matter how much time I leave the page opened. When I connect from the device, it reconnects as the different client all the time. So, device log is full of "You have connected" messages. At the same time my desktop browser is reporting that clients are connecting and disconnecting after 20 seconds:

```
You have connected
http://localhost/:18User 2 joined
http://localhost/:22User 2 left
http://localhost/:18User 3 joined
http://localhost/:22User 3 left
http://localhost/:18User 4 joined
```
Logcat output:

```
> adb.exe logcat -v raw browser:I *:S
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
Console: You have connected http://192.168.1.100/:14
...
```
Note that I'm not navigating around or reloading the page... It just stays opened
### My blind guess.
It seems that android client code fails to provide the same identifier when he makes the new connection to the server. The server thinks that this is the entirely new client, and fires the "new client" routine. That's a blind guess. I'm trying to debug the client code now, but it is kinda slow on Android.

If someone knows how to get rid of this issue, please let me know.