react-native: Unexpected Websocket fail with big messages on Android

Description

I have a Websocket implementation that needs to send big chunks of Data at times.

The biggest chunk is a string with a length of 26858790 characters.

On iOS this is not a problem, but when running the same code on Android the Socket hangs up automatically with code 1001:

{"code": 1001, "isTrusted": false, "reason": ""}

This indicates to me, that there is some default setting in the Android implementation of Websockets. Sadly I don’t have obvious access to that via the provided react-native Websocket API.

Please help! 😃

React Native Version

0.72.5

Output of npx react-native info

System:
  OS: macOS 13.5.2
  CPU: (10) arm64 Apple M1 Pro
  Memory: 70.63 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.18.0
    path: ~/.volta/tools/image/node/18.18.0/bin/node
  Yarn:
    version: 1.22.19
    path: ~/.volta/tools/image/yarn/1.22.19/bin/yarn
  npm:
    version: 9.8.1
    path: ~/.volta/tools/image/node/18.18.0/bin/npm
  Watchman:
    version: 2023.06.12.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.12.1
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 22.2
      - iOS 16.2
      - macOS 13.1
      - tvOS 16.1
      - watchOS 9.1
  Android SDK:
    API Levels:
      - "29"
      - "30"
      - "33"
    Build Tools:
      - 30.0.3
      - 33.0.0
    System Images:
      - android-33 | Google APIs ARM 64 v8a
      - android-33 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2022.3 AI-223.8836.35.2231.10406996
  Xcode:
    version: 14.2/14C18
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 11.0.20
    path: /usr/bin/javac
  Ruby:
    version: 2.7.8
    path: /opt/homebrew/opt/ruby@2.7/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.72.5
    wanted: 0.72.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Steps to reproduce

  1. Setup a react-native project
  2. Create Websocket
const ws = new WebSocket(...)
  1. Somewhere add a snippet like this:
      let myMsg = '';
      while (newmsg.length < 26858780) {
        myMsg = myMsg + "a";
      }


    ws.send(JSON.stringify({type: "inProgress", message: myMsg});

This will produce the described error

Snack, screenshot, or link to a repository

https://github.com/lauhon/react-native-android-websocket-bug

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

I think this might end badly. OkHttp’s cap is intended to limit queueing on the web socket. If you need to send a large payload, you should do it on a regular HTTP post, and send control messages on the web socket?

As is this will disrupt the latency of the web socket while the oversized message is being transmitted.

Hey @cortinico, quick update:

okHttp, which is used for networking on android, has a hard cap for messages of 16MiB

I opened a PR on their repo which would make the max message size configurable.

How should we continue here on react native side if/after they merged it? Should people be able to configure this on JS side, or should there be a hard cap? I assume on iOS there is also some hard cap…

For reference: ws enables people to configure it and they have a default hard cap set to 100MiB

Nvm. after some digging i found this module

Will look into it and come back later