← All Insights

Socket.io Not Working on Android 9 (API 28)

A common issue when developing real-time Android apps: Socket.io works fine on older devices but completely stops working on Android 9 (API 28) and above. The socket appears to connect but emitted events never reach the server.

This is not a Socket.io bug — it is an Android OS-level security change introduced in API 28.


The Cause

Android 9 introduced a platform-level restriction on cleartext (unencrypted HTTP) network traffic. Prior to API 28, cleartext traffic was allowed by default. From API 28 onwards, it is blocked by default for all apps targeting that API level or higher.

Socket.io connections that use http:// (plain WebSocket over HTTP) are considered cleartext traffic. The connection appears to succeed at the socket layer, but data transmission is blocked — so events are emitted on the client but never arrive at the server.

This restriction affects HTTP, FTP, unencrypted WebSockets (ws://), and media streams over plain HTTP.


The Fix for Testing

To allow cleartext traffic during development and testing, add the android:usesCleartextTraffic attribute to the <application> tag in your AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:usesCleartextTraffic="true">

    <!-- activities, services, etc. -->

</application>

After adding this attribute, clean and rebuild the project. Socket.io events will now transmit correctly on Android 9+.


The Correct Fix for Production

The usesCleartextTraffic flag is a testing workaround, not a production solution. In production, switch your Socket.io server to use HTTPS and WSS:

  • Configure your Node.js Socket.io server with SSL (or place it behind an Nginx reverse proxy with SSL termination)
  • Change the connection URL in your Android app from http:// to https://
  • Remove android:usesCleartextTraffic="true" from the manifest

Example connection update:

// Before
IO.socket("http://your-server.com:3000", opts);

// After
IO.socket("https://your-server.com", opts);

Fine-Grained Control with Network Security Config

If you need to allow cleartext for specific domains only (useful when third-party services require it), use Android's Network Security Configuration instead of the global flag:

<!-- res/xml/network_security_config.xml -->
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your-dev-server.com</domain>
    </domain-config>
</network-security-config>

Reference it in the manifest:

<application
    android:networkSecurityConfig="@xml/network_security_config">
</application>

Conclusion

Socket.io failures on Android 9+ are caused by the platform's cleartext traffic restriction, not a library bug. For development, android:usesCleartextTraffic="true" restores the old behavior quickly. For production, migrate to HTTPS/WSS — this is the correct fix and removes the need for the manifest flag entirely.