Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

    I got binary response from websocket. How to convert from binary to JSON?

    Bugs
    0
    3
    54
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      muthukumardab last edited by

      I got the below response as binary that needs to be converted into JSON / string.

      <Buffer 01 01 31 30 36 32 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b7 97 cb 00 00 00 00 00 4e ff 5b 2e 86 01 00 00 88 07 02 00 00 00 00 ... 1 more byte>

      G J 2 Replies Last reply Reply Quote 0
      • G
        GaikwadVikas @muthukumardab last edited by

        @muthukumardab Have you implemented NodeJS or JS websocket?
        If yes, is it possible to share code?

        1 Reply Last reply Reply Quote 0
        • J
          Jeet Pattani @muthukumardab last edited by

          @muthukumardab
          Try the below code

          //Working Code which fetches prices of reliance, sbi, and nifty
          
          const WebSocket = require('ws');
          
          // WebSocket URL
          const url = 'ws://smartapisocket.angelone.in/smart-stream';
          
          // Authentication headers
          const headers = {
            'Authorization': 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VybmFtZSI6IlA1Mjk3NzQiLCJyb2xlcyI6MCwidXNlcnR5cGUiOiJVU0VSIiwiaWF0IjoxNjg2NzE2ODYwLCJleHAiOjE2ODY4MDMyNjB9.WmoIdanOGGXox-We42Bre-qSK6yyZQlcci4h3JnLxc8Dt7WgmJZa7IzergbdB2VWPviVg7uKWAi-rOr0H5ijQw',
            'x-api-key': 'erZDHFHD',
            'x-client-code': 'P529774',
            'x-feed-token': 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VybmFtZSI6IlA1Mjk3NzQiLCJpYXQiOjE2ODY3MTY4NjAsImV4cCI6MTY4NjgwMzI2MH0.fWOQ57fcTGqzuVlktvzFYVe7ztZrt8TFIkrLXvX-Z2HqOtR3cSxsDRW9uv8a9YSCKxt348gAmSondkaHmGaBBA'
          };
          
          // Create a WebSocket instance
          const socket = new WebSocket(url, { headers });
          
          // Handle WebSocket connection open
          socket.on('open', () => {
            console.log('WebSocket connected');
          
            // Send a subscription request
            const subscriptionRequest = {
              //correlationID: '',
              action: 1,
              params: {
                mode: 1,
                tokenList: [
                  {
                    exchangeType: 1,
                    tokens: ['26000','2885','3045','99926009','99926011']
          
                    /*
                    NIFTY-50: 26000
                    RELIANCE-EQ: 2885
                    SBIN-EQ: 3045
                    NIFTY BANK:99926009
                    NIFTY MIDCAP 100:99926011
                    SENSEX: 99919000
          
                    */
          
                  },
                 /* {
                    exchangeType: 5,
                    tokens: ['234230', '234235', '234219']
                  }*/
                ]
              }
            };
          
            socket.send(JSON.stringify(subscriptionRequest));
          });
          
          // Handle received messages from the WebSocket
          socket.on('message', (message) => {
            // Parse the received binary data based on the provided response contract
          
            // Example: Extract the Last Traded Price (LTP) from the received data
            const data = new Uint8Array(message);
            const ltpBytes = data.slice(43, 47);
            const ltpValue = ltpBytes.reduce((value, byte, index) => value + byte * Math.pow(256, index), 0);
            const ltp = ltpValue / 100;
          
            console.log('Received LTP:', ltp);
          });
          
          // Handle WebSocket connection close
          socket.on('close', () => {
            console.log('WebSocket connection closed');
          });
          
          // Send heartbeat message every 30 seconds to keep the connection alive
          setInterval(() => {
            if (socket.readyState === WebSocket.OPEN) {
              socket.send('ping');
            }
          }, 30000);
          
          
          

          P.S. I also had one question which I am not able to find answer to, how to get the price for SENSEX I am not able to find the correct token for it. Please do tell if you know how to get the ltp for sensex.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post