Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native and using websockets

Tags:

react-native

I have such code with [email protected]

import React, { AsyncStorage, Component, View, Text, WebView, WebSocket } from 'react-native';
import { Avatar, Divider, Subheader, COLOR } from 'react-native-material-design';

export default class Avatars extends Component {
    _setWebSocket = (endpoint, sessionToken) => {
      const ws = new WebSocket('wss://' + endPoint + '/api/live?authToken=' + sessionToken);
      console.log('Setting socket: ' + 'wss://' + endpoint + '/api/live?authToken=' + sessionToken);

and I'm calling code from:

componentDidMount() {
   this._setWebSocket(endpoint, token);
 }

I have issue with trying to get WebSockets to be working as code:

console.log('Setting socket: ' + 'wss://' + endpoint + '/api/live?authToken=' + sessionToken); 

never is triggered . If I put console log before creating new WebSocket it runs. What I'm doing wrong and how can I debug as chrome isn't showing useful information in combination with Genymotion.

like image 262
MarJano Avatar asked Apr 09 '26 17:04

MarJano


1 Answers

Issue was easy to find and probably I was tired

import React, { AsyncStorage, Component, View, Text, WebView, WebSocket } from 'react-native';

I put WebSocket to be imported but this is native module ... So removing WebSocket from import dependencies helped.

like image 135
MarJano Avatar answered Apr 12 '26 08:04

MarJano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!