I’m developing an app in React Native and I want to implement a WebSocket, but the WebSocket events come with isTrusted set to false, and I need it to be true.
When I test the same function in the browser console, isTrusted is true. Does anyone know what I could be doing wrong?
I’m using expo and expo go on my android phone to test
snack: https://snack.expo.dev/@pedroaf0/websocket-in-react-native-returns-istrusted-false-expo-go-android?platform=android
Code:
import { StyleSheet, TouchableOpacity, Text, View } from 'react-native';
export default function app() {
function connectWebSocket() {
console.log('Conectando ao WebSocket...');
const socket = new WebSocket('wss://echo.websocket.org');
// Evento disparado quando a conexão é aberta
socket.addEventListener('open', function (event) {
console.log('Conexão aberta com sucesso.');
// Envia uma mensagem para o servidor WebSocket
socket.send('Olá, servidor WebSocket!');
});
// Evento disparado quando uma mensagem é recebida do servidor
socket.addEventListener('message', function (event) {
console.log('Mensagem recebida do servidor:', event.data);
console.log(event);
});
// Evento disparado quando ocorre um erro na conexão WebSocket
socket.addEventListener('error', function (event) {
console.error('Erro na conexão WebSocket:', event);
});
// Evento disparado quando a conexão é fechada
socket.addEventListener('close', function (event) {
console.log('Conexão WebSocket fechada:', event);
});
}
return (
<View style={styles.conteiner}>
<TouchableOpacity style={styles.button} onPress={connectWebSocket}>
<Text style={styles.buttonText}>conectar</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
button: {
backgroundColor: '#007AFF',
borderRadius: 8,
padding: 10,
marginTop: 10,
},
buttonText: {
color: 'white',
textAlign: 'center',
},
conteiner: {
flex: 1,
alignContent: 'center',
justifyContent: 'center',
},
});
Logs in RN:
LOG Connecting to WebSocket...
LOG Connection opened successfully.
LOG Message received from server: Request served by 7811941c69e658
LOG {"data": "Request served by 7811941c69e658", "isTrusted": false}
LOG Message received from server: Hello, WebSocket server!
LOG {"data": "Hello, WebSocket server!", "isTrusted": false}
The same function executed in the browser returns in the logs:
connectWebSocket()
Connecting to WebSocket...
Connection opened successfully.
Message received from server: Request served by 7811941c69e658
MessageEvent {isTrusted: true, data: 'Request served by 7811941c69e658', origin: 'wss://echo.websocket.org', lastEventId: '', source: null, …}
Message received from server: Hello WebSocket server!
MessageEvent {isTrusted: true, data: 'Hello, WebSocket server!', origin: 'wss://echo.websocket.org', lastEventId: '', source: null, …}
What is the difference?
How to connect to a WebSocket in React Native android in a Trusted way (isTrusted)?
I tried variations of this code, but it didn’t make a difference:
const options = {
headers: {
'Sec-WebSocket-Protocol': 'echo-protocol',
},
rejectUnauthorized: false,
};
const ws = new WebSocket('wss://echo.websocket.org', [], options);
Pedro Afonso M. Nunes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.