Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Socket.IO on Android keeps failing with xhr poll error

Errorconnect_error: xhr poll error

Short 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.

  • Applies to socket.io-client Java 2.x, Android 14, Android 15, Socket.IO 4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1curl 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. 2The server is reachable from the phone browser over HTTPS only, and the app uses an http:// or ws:// URL.Cleartext HTTP is blockedFix
    3. 3No request leaves the app at any URL.The INTERNET permission is missingFix

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 --list

Confirmed by developer.android.com

Did this fix it?

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 installDebug

Confirmed by developer.android.com

Did this fix it?

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.xml

2Fix

Add the permission to AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />

3Verify

Reinstall the app and connect again.

./gradlew installDebug

Confirmed by socket.io

Did this fix it?

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:

Related fixes

Mobile 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.