How to connect to a WebSocket in React Native android with isTrusted:true? WebSocket in React Native returns “isTrusted”: false (expo go, android)

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);

New contributor

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật