Navigation

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

    The Below JS Code Works

    General Discussion
    0
    1
    13
    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.
    • J
      Jeet Pattani last edited by

      Re: WebSocket 2.0 Streaming is not Receiving the Live Price Feed

      //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 JWT_TOKEN',
        'x-api-key': 'erZDHFHD',
        'x-client-code': 'P529774',
        'x-feed-token': 'FEED_TOKEN'
      };
      
      // 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);
      
      
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post