Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket on android works slower than on ios #44292

Open
hawkf opened this issue Apr 26, 2024 · 1 comment
Open

WebSocket on android works slower than on ios #44292

hawkf opened this issue Apr 26, 2024 · 1 comment

Comments

@hawkf
Copy link

hawkf commented Apr 26, 2024

Description

My server code:
const WebSocket = require('ws');

// Create a WebSocket server
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
console.log('Client connected');

// Send 3000 messages to the client
for (let i = 0; i < 3000; i++) {
    ws.send(`Message ${i + 1}`);
}

});

React native component code:
/**

import React, {useEffect} from 'react';
import type {PropsWithChildren} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';

import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

type SectionProps = PropsWithChildren<{
title: string;
}>;

function Section({children, title}: SectionProps): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';
useEffect(() => {
const ws = new WebSocket('ws://192.168.88.180:8080');

ws.onopen = () => {
  console.log('Connected to WebSocket server');
};

ws.onmessage = e => {
  console.log('Received message:', e.data);
};

ws.onerror = error => {
  console.error(`WebSocket error: ${error}`);
};

ws.onclose = () => {
  console.log('Disconnected from WebSocket server');
};

return () => {
  ws.close();
};

}, []);
return (

<Text
style={[
styles.sectionTitle,
{
color: isDarkMode ? Colors.white : Colors.black,
},
]}>
{title}

<Text
style={[
styles.sectionDescription,
{
color: isDarkMode ? Colors.light : Colors.dark,
},
]}>
{children}


);
}

function App(): React.JSX.Element {
const isDarkMode = useColorScheme() === 'dark';

const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};

return (

<StatusBar
barStyle={isDarkMode ? 'light-content' : 'dark-content'}
backgroundColor={backgroundStyle.backgroundColor}
/>


<View
style={{
backgroundColor: isDarkMode ? Colors.black : Colors.white,
}}>

Edit App.tsx to change this
screen and then come back to see your edits.








Read the docs to discover what to do next:





);
}

const styles = StyleSheet.create({
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
},
highlight: {
fontWeight: '700',
},
});

export default App;

=====================

In ios device I receive 3 messages every 1ms but in android i receive 1 message every 2ms.

Can I improve this?

It's important for me because I develop application with real time exchange.

Steps to reproduce

  1. Install application
  2. Start the server
  3. Launch application
  4. See logs

React Native Version

0.73.4

Affected Platforms

Runtime - Android

Output of npx react-native info

System:
  OS: macOS 14.0
  CPU: (8) arm64 Apple M1 Pro
  Memory: 100.56 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.17.0
    path: ~/.nvm/versions/node/v18.17.0/bin/node
  Yarn: Not Found
  npm:
    version: 9.6.7
    path: ~/.nvm/versions/node/v18.17.0/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.0
      - iOS 17.0
      - macOS 14.0
      - tvOS 17.0
      - watchOS 10.0
  Android SDK: Not Found
IDEs:
  Android Studio: 2022.3 AI-223.8836.35.2231.10811636
  Xcode:
    version: 15.0/15A240d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /usr/bin/javac
  Ruby:
    version: 2.7.5
    path: /Users/admin/.rvm/rubies/ruby-2.7.5/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.4
    wanted: 0.73.4
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Stacktrace or Logs

Received message: Message 152
00:55:00.926 Received message: Message 98
00:55:00.928 Received message: Message 99
00:55:00.932 Received message: Message 114
00:55:00.936 Received message: Message 275
00:55:00.941 Received message: Message 153
00:55:00.943 Received message: Message 100
00:55:00.946 Received message: Message 115
00:55:00.949 Received message: Message 276
00:55:00.951 Received message: Message 101

Reproducer

https://github.com/hawkf/reactNativeWebSocket.git

Screenshots and Videos

No response

Copy link

⚠️ Newer Version of React Native is Available!
ℹ️ You are on a supported minor version, but it looks like there's a newer patch available - 0.73.7. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@github-actions github-actions bot added Platform: Android Android applications. Platform: iOS iOS applications. labels Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant