Socket.IO on Android keeps failing with xhr poll error
connect_error: xhr poll errorShort answer
xhr poll error means the first HTTP long-polling request never completed, so the server was not reached. Test the server's polling URL with curl. Then check the three usual Android causes: the app uses localhost instead of 10.0.2.2 on the emulator, plain HTTP is blocked, or the INTERNET permission is missing.
Fix it fast
1Find out which cause you have
Check that the Socket.IO server answers over HTTP from your computer. A working server returns a body starting with 0 and a JSON sid.
curl "http://localhost:3000/socket.io/?EIO=4&transport=polling"2Match what you see, then jump to its fix
- 1
curl works on your computer, but the app's connect_error appears at once and nothing reaches the server.The app uses localhost on the emulatorFix - 2
The server is reachable from the phone browser over HTTPS only, and the app uses an http:// or ws:// URL.Cleartext HTTP is blockedFix - 3
No request leaves the app at any URL.The INTERNET permission is missingFix
- 1
What does the error look like?
connect_error: xhr poll error
Cause 1 of 3
The app uses localhost on the emulator
1Check
Inside the emulator, 127.0.0.1 is the emulated device. The host loopback is the alias 10.0.2.2.
grep -rn "IO.socket" app/src/main/2Fix
Point the client at 10.0.2.2 on the emulator, or forward the port with adb reverse.
val socket = IO.socket("http://10.0.2.2:3000")
socket.connect()3Verify
The server logs a new connection when the app starts.
adb reverse --listConfirmed by developer.android.com
Cause 2 of 3
Cleartext HTTP is blocked
1Check
Apps that target Android 9 (API 28) or higher have cleartext traffic disabled by default.
grep -rn "cleartext\|networkSecurityConfig" app/src/main/2Fix
Use an https:// URL in production. For a development host, allow cleartext for that domain only in a network security config.
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>3Verify
Rebuild and reconnect; connect_error no longer shows.
./gradlew installDebugConfirmed by developer.android.com
Cause 3 of 3
The INTERNET permission is missing
1Check
The Socket.IO Android guide says the manifest must hold the internet permission.
grep -n "INTERNET" app/src/main/AndroidManifest.xml2Fix
Add the permission to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />3Verify
Reinstall the app and connect again.
./gradlew installDebugConfirmed by socket.io
How do I stop it coming back?
- Keep the server URL in a build config value per build type.
- Log every connect_error argument while developing, because the reason string is the only hint.
- If a proxy sits in front of the server, check that it forwards the WebSocket headers.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
The Android emulator times out when calling a server on your own computer
the same URL works in the browser on your computer but not in the app
Inside the emulator, 127.0.0.1 and localhost point to the emulated phone, not to your computer.
4 causes, each with a fixMobile apps
Android app crashes because the Apache HTTP classes are missing
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fix