title: communicating with socket.io from android
body: <h2> Problem Summary</h2>
  
  <p>I am trying to bundle a simple socket.io chat app into a native app to run on Android 2.2 and above using phonegap.</p>
  
  <h2>socket.io web client --> socket.io server works</h2>
  
  <p>I have a simple server running socket.io which successfully communicates with a socket.io client when its run in a web browser.</p>
  
  <h2>socket.io mobile client in mobile browser --> socket.io server works</h2>
  
  <p>Note that <a href="http://caniuse.com/websockets" rel="nofollow noreferrer">because websockets arent supported on Android 2.2</a> it defaults back to xhr polling, sample messages from server when connecting through the web browser on Android 2.2
  <code><pre>[root@webnode-2 nodechat]# node server.js
     info  - socket.io started
     debug - client authorized
     info  - handshake authorized 6036976111002307981
     debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313758
     debug - setting poll timeout
     debug - client authorized for
     debug - clearing poll timeout
     debug - xhr-polling writing 1::
     debug - set close timeout for client 6036976111002307981
  6036976111002307981
     debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313839
     debug - setting poll timeout
     debug - clearing poll timeout
     debug - xhr-polling writing 5:::{"name":"message","args":["Welcome stranger!"]}
     debug - set close timeout for client 6036976111002307981
     debug - discarding transport
     debug - cleared close timeout for client 6036976111002307981
     debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184313928
     debug - setting poll timeout
     debug - discarding transport
     debug - cleared close timeout for client 6036976111002307981
     debug - clearing poll timeout
     debug - xhr-polling writing 8::
     debug - set close timeout for client 6036976111002307981
     debug - xhr-polling closed due to exceeded duration
     debug - setting request GET /socket.io/1/xhr-polling/6036976111002307981?t=1332184334139
     debug - setting poll timeout
     debug - discarding transport
     debug - cleared close timeout for client 6036976111002307981
     debug - xhr-polling received data packet 5:::{"name":"connect_friends","args":[{"user_id":"2","friends_list":[1,2,3,4,5,6,7,8,9,10]}]}</p>
  
  <hr>
  
  <p>REGISTER USER</p>
  
  <hr>
  
  <p>{ user_id: '2',
    friends_list: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] }
  10
  assigned user_id: 2 to socket 6036976111002307981
  </pre></code></p>
  
  <h2>socket.io from phonegap --> socket.io server fails</h2>
  
  <p>However when I try to connect with socket.io in phonegap the connection is opened but quickly closed. </p>
  
  <p>In the android log it has an error about requirin flash player >v10 but my phone has flash player v 11.1
  <code><pre>
  03-19 11:38:46.847: I/PhoneGapLog(18469): Found log level DEBUG
  03-19 11:38:46.847: I/PhoneGapLog(18469): Changing log level to DEBUG(3)
  03-19 11:38:46.847: D/DroidGap(18469): DroidGap.onCreate()
  03-19 11:38:46.847: D/DroidGap(18469): DroidGap.loadUrl(file:///android_asset/www/index.html)
  03-19 11:38:46.847: D/DroidGap(18469): DroidGap: url=file:///android_asset/www/index.html baseUrl=file:///android_asset/www/
  03-19 11:38:46.847: D/DroidGap(18469): DroidGap.init()
  03-19 11:38:46.941: D/dalvikvm(18469): GC_FOR_MALLOC freed 6234 objects / 618952 bytes in 68ms
  03-19 11:38:46.968: D/SoftKeyboardDetect(18469): Ignore this event
  03-19 11:38:47.007: D/SoftKeyboardDetect(18469): Ignore this event
  03-19 11:38:47.058: D/SoftKeyboardDetect(18469): Ignore this event</p>
  
  <p><b><i>03-19 11:38:47.241: D/PhoneGapLog(18469): Flash Player >= 10.0.0 is required.
  03-19 11:38:47.241: D/PhoneGapLog(18469): file:///android_asset/www/js/socket.io/socket.io.js: Line 2622 : Flash Player >= 10.0.0 is required.
  03-19 11:38:47.241: E/Web Console(18469): Flash Player >= 10.0.0 is required. at file:///android_asset/www/js/socket.io/socket.io.js:2622</i></b></p>
  
  <p>03-19 11:38:47.796: D/PhoneGapLog(18469): Receiving Welcome stranger!
  03-19 11:38:47.796: D/PhoneGapLog(18469): file:///android_asset/www/index.html: Line 8 : Receiving Welcome stranger!
  03-19 11:38:47.796: I/Web Console(18469): Receiving Welcome stranger! at file:///android_asset/www/index.html:8
  03-19 11:39:07.007: E/DroidGap(18469): DroidGap: TIMEOUT ERROR! - calling webViewClient
  03-19 11:39:07.007: D/Cordova(18469): DroidGap: GapViewClient.onReceivedError: Error code=-6 Description=The connection to the server was unsuccessful. URL=file:///android_asset/www/index.html
  03-19 11:39:07.167: D/SoftKeyboardDetect(18469): Ignore this event
  03-19 11:39:16.323: D/dalvikvm(18469): GC_EXPLICIT freed 6130 objects / 505920 bytes in 156ms
  </pre></code></p>
  
  <p>The log of events on the server is 
  <code><pre>
  [root@webnode-2 nodechat]# node server.js
     info  - socket.io started
     debug - client authorized
     info  - handshake authorized 1009349893764580916
     debug - setting request GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327502
     debug - setting poll timeout
     debug - client authorized for
     debug - clearing poll timeout
     debug - xhr-polling writing 1::
     debug - set close timeout for client 1009349893764580916
     debug - setting request GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327602
     debug - setting poll timeout
     debug - clearing poll timeout
     debug - xhr-polling writing 5:::{"name":"message","args":["Welcome stranger!"]}
     debug - set close timeout for client 1009349893764580916
     debug - discarding transport
     debug - cleared close timeout for client 1009349893764580916
     debug - setting request GET /socket.io/1/xhr-polling/1009349893764580916?t=1332182327802
     debug - setting poll timeout
     debug - discarding transport
     debug - cleared close timeout for client 1009349893764580916
     debug - clearing poll timeout
     info  - transport end
     debug - set close timeout for client 1009349893764580916
     debug - cleared close timeout for client 1009349893764580916</p>
  
  <hr>
  
  <p>DISCONNECTED EVENT</p>
  
  <hr>
  
  <p>debug - discarding transport
     debug - client authorized
     info  - handshake authorized 2044675477593417130
     debug - setting request GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182347938
     debug - setting poll timeout
     debug - client authorized for
     debug - clearing poll timeout
     debug - xhr-polling writing 1::
     debug - set close timeout for client 2044675477593417130
     debug - setting request GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348112
     debug - setting poll timeout
     debug - clearing poll timeout
     debug - xhr-polling writing 5:::{"name":"message","args":["Welcome stranger!"]}
     debug - set close timeout for client 2044675477593417130
     debug - discarding transport
     debug - cleared close timeout for client 2044675477593417130
     debug - setting request GET /socket.io/1/xhr-polling/2044675477593417130?t=1332182348308
     debug - setting poll timeout
     debug - discarding transport
     debug - cleared close timeout for client 2044675477593417130
     debug - clearing poll timeout
     info  - transport end
     debug - set close timeout for client 2044675477593417130
     debug - cleared close timeout for client 2044675477593417130</p>
  
  <hr>
  
  <p>DISCONNECTED EVENT</p>
  
  <hr>
  
  <p>debug - discarding transport
  </pre></code></p>
  
  <p>The message from the server saying "Welcome stranger!" is received by the phone. On the phone the dialog saying Application Error, The connection to the server was unsuccessful. (file:///android_asset/www/index.html)</p>
  
  <h2>phone gap websockets client --> socket.io server fails</h2>
  
  <p>I then read that websockets wasn't supported in phonegap yet, and came across this project for <a href="http://anismiles.wordpress.com/2011/02/03/websocket-support-in-android%E2%80%99s-phonegap-apps/" rel="nofollow noreferrer">WebSocket support in Android’s Phonegap apps</a>. When I got this running and tried to connect on the socket.io server and I got the error</p>
  
  <p><code><pre>[root@webnode-2 nodechat]# node server.js
     info  - socket.io started
     debug - destroying non-socket.io upgrade
  </pre></code></p>
  
  <p><a href="https://stackoverflow.com/questions/6634940/websocket-problem-cannot-connect-to-node-js-server">which i believe is due to trying to connect to a socket.io server using websockets without using the socket.io client js</a>. I tested it with just a node.js server and it worked</p>
  
  <h2>Questions</h2>
  
  <ol>
  <li>Is there a way to tell socket.io about the websocket created using <a href="http://anismiles.wordpress.com/2011/02/03/websocket-support-in-android%E2%80%99s-phonegap-apps/" rel="nofollow noreferrer">WebSocket support in Android’s Phonegap apps</a>? </li>
  <li>Why is the xhr polling timing out?</li>
  </ol>