WebSocket on android works slower than on ios
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: /**
- Sample React Native App
- https://github.com/facebook/react-native
- @format */
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 ( <View style={styles.sectionContainer}> <Text style={[ styles.sectionTitle, { color: isDarkMode ? Colors.white : Colors.black, }, ]}> {title} </Text> <Text style={[ styles.sectionDescription, { color: isDarkMode ? Colors.light : Colors.dark, }, ]}> {children} </Text> </View> ); }
function App(): React.JSX.Element { const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = { backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, };
return ( <SafeAreaView style={backgroundStyle}> <StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} backgroundColor={backgroundStyle.backgroundColor} /> <ScrollView contentInsetAdjustmentBehavior="automatic" style={backgroundStyle}> <Header /> <View style={{ backgroundColor: isDarkMode ? Colors.black : Colors.white, }}> <Section title="Step One"> Edit <Text style={styles.highlight}>App.tsx</Text> to change this screen and then come back to see your edits. </Section> <Section title="See Your Changes"> <ReloadInstructions /> </Section> <Section title="Debug"> <DebugInstructions /> </Section> <Section title="Learn More"> Read the docs to discover what to do next: </Section> <LearnMoreLinks /> </View> </ScrollView> </SafeAreaView> ); }
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
- Install application
- Start the server
- Launch application
- 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
| :warning: | Newer Version of React Native is Available! |
|---|---|
| :information_source: | 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. |