N
N
Ne7Le4Der2021-09-08 11:25:02
Node.js
Ne7Le4Der, 2021-09-08 11:25:02

What is the difference between React-native WebSocket and NodeJS ws?

NodeJS:

const WebSocket = require('ws');
const ws = new WebSocket('wss://siteurl');

ws.on('open', function open() {
    ws.send('');
});

ws.on('message', function incoming(message) {
    console.log(message);
});


React Navite:
let connection = new WebSocket('wss://siteurl');

connection.onopen = () => {
            connection.send('');
        };

connection.onmessage = ((event) => {
            console.log(event);
        });


The NodeJS code works fine, the server returns a response. On react native, I get the following in response
{"data": [], "isTrusted": false}

. What could be the problem? I run the application on the Android Studio emulator, via react-native run-android

UPD:
It also works fine in the browser
const socket = new WebSocket('wss://siteurl');
        
        socket.onopen = () => {
            console.log('Socket connected!');
            socket.send('');
        }

        socket.onmessage = (message) => {
            console.log(`Message:\n`, message.data);
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question