Getting empty object on websocket (Market data feed)

Hi,
I have integrate websocket with React Native. I got websocket response properly with node JS but when I try it with React Native, websocket connection is established but it return empty object i.e, {“isTrusted”:false,“data”:{}} not even binary encoded message.

Please provide solution or reason why I am getting empty object not the encoded message. Also provide how to decode the message after receiving non-empty object in React Native.

Thank you

My implementation code:

import React, {useEffect} from 'react';
import {StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import {Buffer} from 'buffer';

const Upstox = () => {

  useEffect(() => {
    connectWebSocket();
  }, []);

  const getUrl = () => {
    return new Promise(async (resolve, reject) => {
      const apiUrl = 'https://api-v2.upstox.com/feed/market-data-feed/authorize';
      const auth_token = 'AUTH_TOKEN';
      let headers = {
        'Content-type': 'application/json',
        Authorization: 'Bearer ' + auth_token,
      };

      try {
        // Make the API request
        const response = await fetch(apiUrl, {
          method: 'GET',
          headers: headers,
        });

        if (!response.ok) {
          throw new Error('Network response was not ok');
        }

        const res = await response.json();
        const url = res.data.authorizedRedirectUri;
        resolve(url);
      } catch (error) {
        console.error('Error:', error);
        reject(error);
      }
    });
  };

  const connectWebSocket = async () => {
    const wsUrl = await getUrl();
    return new Promise((resolve, reject) => {
      const ws = new WebSocket(wsUrl);

      ws.addEventListener('open', () => {
        console.log('connected');
        resolve(ws);

        setTimeout(() => {
          const data = {
            guid: 'someguid',
            method: 'sub',
            data: {
              mode: 'full',
              instrumentKeys: ['NSE_EQ|INE155A01022'],
            },
          };
          ws.send(Buffer.from(JSON.stringify(data)));
        }, 1000);
      });

      ws.addEventListener('close', () => {
        console.log('disconnected');
      });

      ws.addEventListener('message', data => {
        // let res = JSON.stringify(decodeProfobuf(data));
        let res = JSON.stringify(data);
        console.log(res); // gives {"isTrusted":false, "data":{}} empty object
      });

      ws.addEventListener('error', error => {
        console.log('error:', error);
        reject(error);
      });
    });
  };
  return (
    <View style={styles.container}>
      <TouchableOpacity onPress={() => connectWebSocket()}>
        <Text style={{fontSize: 22, fontWeight: 'bold', color: 'black'}}>
          Upstox WebSocket
        </Text>
      </TouchableOpacity>
    </View>
  );
};

We are currently evaluating this matter and will soon provide a complete React JS code. Thank you for your understanding and patience.

HI @Pradeep_Jaiswar,
When will I get a solution for this problem ?

and few questions…

and a few questions…
I am developing a public stock market app in React Native, and for that, I required real-time data from Websocket. So…

  • Is it ok that multiple users are getting the market data with the same authentication token of a particular user ?
  • Is there any limitation on the number of socket connections at a time for a particular auth_token (a single user) ?
  • As I can get only 100 instrument key data from a websocket at a time, if I use the NodeJS server as middleware for Upstox websocket and React Native, this 100 instrument key limitation will affect the functionality of my app because multiple users are getting the data of different instrument keys. So how can I connect a web socket directly to a React Native or React app? Also, is there any limitation to socket connections ?
  • Can I get top market gainer and looser data from the Upstox API?

We’ve included a sample code in our ReactJS repository that we believe will be helpful to you.
Access the sample here

  • Using the same authentication token of a specific user to access market data is not advisable and not recommended.
  • A limit of three websocket connections per authentication will be implemented soon, so please take note of this.
  • Efforts are underway to raise the limit on websocket token numbers. Ensure you stay updated and incorporate this information into the documentation.
  • Data on the top market gainers and losers is currently unavailable through the API.