# IonicFramework v8 cannot use WebSocket correctly in Android. Error code 1006, but it runs normally in Chrome

**URL:** <https://forum.ionicframework.com/t/ionicframework-v8-cannot-use-websocket-correctly-in-android-error-code-1006-but-it-runs-normally-in-chrome/248087>\
**Category:** Ionic Framework\
**Created:** [June 4, 2025, 7:41am UTC](https://forum.ionicframework.com/t/ionicframework-v8-cannot-use-websocket-correctly-in-android-error-code-1006-but-it-runs-normally-in-chrome/248087 "2025-06-04T07:41:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GLK](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/glk/32/149168_2.png) [@GLK](https://forum.ionicframework.com/u/GLK)\
**Post date:** [June 4, 2025, 7:41am UTC](https://forum.ionicframework.com/t/ionicframework-v8-cannot-use-websocket-correctly-in-android-error-code-1006-but-it-runs-normally-in-chrome/248087/1 "2025-06-04T07:41:53Z")

</div>

```tsx
useEffect(() => {
    const ws = new WebSocket("ws://192.168.2.174:9090");

    ws.onmessage = (event) => {
      const data = JSON.parse(event.data);
      console.log(data);
    };

    ws.onopen = () => {
      console.log("WebSocket 连接已打开");
    }

    ws.onclose = () => {
      console.log("WebSocket 连接已关闭");
    }

    ws.onerror = (error) => {
      console.log("WebSocket 连接出错", error);
    }

    return () => {
      ws.close();
    };
  }, [])

```

 ![微信图片_20250604153838](https://global.discourse-cdn.com/ionicframework/original/3X/4/c/4c6b0b4d3586a8bc56ff408ef9ad5b1cfa20a4fd.jpeg)

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/b/8/b8ab31f2523dbe3ef6a3de74be88c3418a8ac302.png)

capacitor.config.ts

```json
import type { CapacitorConfig } from '@capacitor/cli';

const config: CapacitorConfig = {
  appId: 'io.ionic.starter',
  appName: 'myApp',
  webDir: 'dist',
  plugins: {},
  server: {
    androidScheme: "http",
  }
};

export default config;

```

package.json

```json
{
  "name": "myApp",
  "private": true,
  "version": "0.0.1",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview",
    "test.e2e": "cypress run",
    "test.unit": "vitest",
    "lint": "eslint"
  },
  "dependencies": {
    "@capacitor/android": "7.2.0",
    "@capacitor/app": "7.0.1",
    "@capacitor/browser": "^7.0.1",
    "@capacitor/core": "7.2.0",
    "@capacitor/haptics": "7.0.1",
    "@capacitor/keyboard": "7.0.1",
    "@capacitor/status-bar": "7.0.1",
    "@ionic/react": "^8.5.0",
    "@ionic/react-router": "^8.5.0",
    "@types/react-router": "^5.1.20",
    "@types/react-router-dom": "^5.3.3",
    "ionicons": "^7.4.0",
    "react": "19.0.0",
    "react-dom": "19.0.0",
    "react-router": "^5.3.4",
    "react-router-dom": "^5.3.4",
    "ros2d": "github:Neoplanetz/ros2djs-ros2",
    "ros3d": "^1.1.0",
    "roslib": "^1.4.1",
    "vconsole": "^3.15.1",
    "zustand": "^5.0.5"
  },
  "devDependencies": {
    "@capacitor/cli": "7.2.0",
    "@testing-library/dom": ">=7.21.4",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^16.2.0",
    "@testing-library/user-event": "^14.4.3",
    "@types/react": "19.0.10",
    "@types/react-dom": "19.0.4",
    "@types/roslib": "^1.3.5",
    "@vitejs/plugin-legacy": "^5.0.0",
    "@vitejs/plugin-react": "^4.0.1",
    "cypress": "^13.5.0",
    "eslint": "^9.20.1",
    "eslint-plugin-react": "^7.32.2",
    "eslint-plugin-react-hooks": "^5.1.0",
    "eslint-plugin-react-refresh": "^0.4.19",
    "globals": "^15.15.0",
    "jsdom": "^22.1.0",
    "terser": "^5.4.0",
    "typescript": "^5.1.6",
    "typescript-eslint": "^8.24.0",
    "vite": "~5.2.0",
    "vitest": "^0.34.6"
  },
  "description": "An Ionic project"
}

```

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [June 4, 2025, 12:54pm UTC](https://forum.ionicframework.com/t/ionicframework-v8-cannot-use-websocket-correctly-in-android-error-code-1006-but-it-runs-normally-in-chrome/248087/2 "2025-06-04T12:54:58Z")

</div>

From what I see in your screenshots, it is working on Android but not Chrome? Your title says it is working in Chrome 🤔

---

<div class="post-metadata">

**Author:** ![GLK](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/glk/32/149168_2.png) [@GLK](https://forum.ionicframework.com/u/GLK)\
**Post date:** [June 5, 2025, 2:36am UTC](https://forum.ionicframework.com/t/ionicframework-v8-cannot-use-websocket-correctly-in-android-error-code-1006-but-it-runs-normally-in-chrome/248087/3 "2025-06-05T02:36:21Z")

</div>

Thank you for your answer! The first image is taken from an Android phone, and the second image is taken from Chrome. The fact is that websocket cannot be linked in Android, but it can be in Chrome. They are the same set of code. What shall I do?

---

<div class="post-metadata">

**Author:** ![GLK](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/glk/32/149168_2.png) [@GLK](https://forum.ionicframework.com/u/GLK)\
**Post date:** [June 5, 2025, 3:45am UTC](https://forum.ionicframework.com/t/ionicframework-v8-cannot-use-websocket-correctly-in-android-error-code-1006-but-it-runs-normally-in-chrome/248087/4 "2025-06-05T03:45:58Z")

</div>

I have found a solution. Add `android:usesCleartextTraffic="true"` to `AndroidManifest.xml` . See [here](https://stackoverflow.com/questions/76467836/does-the-browser-websocket-work-fine-with-capacitor-cross-platform) and [here](https://forum.ionicframework.com/t/websocket-opening-handshake-was-canceled/212744)

But now my process is that after executing ionic cap sync, ionic cap sync will regenerate the AndroidManifest.xml file, which contains:

```xml
<?xml version='1.0' encoding='utf-8'?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:amazon="http://schemas.amazon.com/apk/res/android">
<application >

</application>

</manifest>

```

Then I need to manually add `android:usesCleartextTraffic="true"`. Where can I directly configure the content of AndroidManifest.xml?

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [June 5, 2025, 11:28am UTC](https://forum.ionicframework.com/t/ionicframework-v8-cannot-use-websocket-correctly-in-android-error-code-1006-but-it-runs-normally-in-chrome/248087/5 "2025-06-05T11:28:37Z")

</div>

When you run `ionic cap run android -l` for debugging, `usesCleartextTraffic` gets auto added to the manifest.

For production, you should be using `wss`.

But, the `capacitor.config.ts` has an option under `server.cleartext` for debugging purposes too. See the notes about it here - [Capacitor Configuration | Capacitor Documentation](https://capacitorjs.com/docs/config#:~:text=cleartext) `cap run` mentioned above actually adds this temporarily which in turn configures the manifest.

You can also set cleartext for specific hosts. See [Unable to carryout HTTP post on Android - #2 by twestrick](https://forum.ionicframework.com/t/unable-to-carryout-http-post-on-android/218979/2)
