Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs
    1. Home
    2. Kavita 0
    3. Posts
    K
    • Profile
    • Following 0
    • Followers 0
    • Topics 6
    • Posts 9
    • Best 1
    • Groups 0

    Posts made by Kavita 0

    • NodeJs websocket is not connecting throwing 401 error

      Hello Team,
      I'm trying to connect websocket via node sdk I tried both the old and new ways defined in this file,

      https://github.com/angel-one/smartapi-javascript

      Error message Im getting,
      
      D:\user\node\jupiter-trader\node_modules\smartapi-javascript\lib\websocket2.0.js:89
                                                                      throw new Error(evt.message);
                                                                      ^
      
      Error: Unexpected server response: 401
          at ws.onerror (D:\user\node\jupiter-trader\node_modules\smartapi-javascript\lib\websocket2.0.js:89:15)
          at callListener (D:\user\node\jupiter-trader\node_modules\ws\lib\event-target.js:290:14)
          at WebSocket.onError (D:\user\node\jupiter-trader\node_modules\ws\lib\event-target.js:230:9)
          at WebSocket.emit (node:events:518:28)
          at emitErrorAndClose (D:\user\node\jupiter-trader\node_modules\ws\lib\websocket.js:1046:13)
          at process.processTicksAndRejections (node:internal/process/task_queues:90:21)
      
      

      Below is the sample code I'm trying to do,

      async function connectWebSocket() {
          try {
              const web_socket = new WebSocketV2({
                  jwttoken: smart_api.access_token, // response from loginAPI
                  apikey: smart_api.api_key, // response from loginAPI
                  clientcode: 'K518180', // my client codo
                  feedtype: 'market_feed',
              });
      
              console.log('🔄 Connecting with WebSocketV2...');
               // Set up event handlers BEFORE connecting
              web_socket.on('open', () => {
                  console.log('✅ WebSocketV2 connected successfully');
              });
              web_socket.on('tick', (data) => {
                  console.log('📈 Market data received:', data);
                  // Process market data for your trading logic
                  // data will contain ltp, volume, etc.
              });
              web_socket.on('error', (err) => {
                  console.error('❌ WebSocketV2 error:', err.message);
                  if (err.message.includes('401')) {
                      console.error('⚠️ Authentication failed! JWT token may be invalid or expired.');
                      console.error('Get a fresh JWT token from generateSession()');
                  }
              });
              web_socket.on('close', () => {
                  console.log('🔌 WebSocketV2 connection closed');
                  // Attempt reconnect after delay
                  setTimeout(() => {
                      console.log('Attempting to reconnect...');
                      connectWebSocket();
                  }, 5000);
              });
      
              // Connect
              console.log('🔄 Establishing WebSocketV2 connection...');
              await web_socket.connect();
              console.log('✅ WebSocketV2 connection established');
      
      
              // Use the format from documentation
              const json_req = {
                  correlationID: 'order_monitor_' + Date.now(),
                  action: 1,  // 1 = subscribe, 0 = unsubscribe
                  mode: 1,
                  exchangeType: 1,
                  tokens: ["market_feed"],  // For order feed
              };
      
              web_socket.fetchData(json_req);
      
              web_socket.on('tick', (data) => {
                  console.log('📦 Order update:', data);
              });
      
              web_socket.on('error', (err) => {
                  console.error('WebSocketV2 error:', err);
              });
      
              return web_socket;
      
          } catch (error) {
              console.error('WebSocketV2 connection failed:', error);
      
              return;
          }
      
      }
      
      
      posted in NodeJS SDK
      K
      Kavita 0
    • Websocket Order status

      Can you please provide working example of websocket order status? Just like you have one for ebsocket streaming.

      Please proovide working example, not link to the existing documentation.

      Thanks!

      posted in Python SDK
      K
      Kavita 0
    • RE: How to get market depth

      Exactly.

      @admin, Is this understanding correct?

      If so, can we expect this functionality any time soon?

      Regards,

      posted in Python SDK
      K
      Kavita 0
    • How to get market depth

      I know that the websocket feed has the dp field to get market depth. But that gives a continuous feed.

      I want to get the depth of a script at a point in time. Not as a feed.

      So a single API call, with the scrip code and the dp task so that I get the depth of the stock at that instant.

      Is it possible?

      posted in Python SDK
      K
      Kavita 0
    • RE: Updates of mw and dp feeds

      @admin

      My client is is D184330 and number is 9769497239. Thanks!

      posted in Python SDK
      K
      Kavita 0
    • Updates of mw and dp feeds

      I have a question regarding the update process for the mw task and dp task.

      I want to know when a new feed from these tasks get generated. So basically, in any stock, there are 2 events

      1. A trade happening (when trade happens automatically order book also changes)
      2. Order book changes, without a trade

      So I want to know when a new feed gets generated for the mw task and the dp task. The reason for the question is that in the mw response, there is best buy and best sell fields. So would a new entry get generated in teh mw feed even without a trade, but when best buy price changes?

      The dp feed I guess would generate a new entry when any one of the 35 parameters in teh response changes.

      So my question is quite simple. Would a new entry in mw feed get generated for a change in best buy/best sell without trade happening?

      Regards,

      posted in Python SDK
      K
      Kavita 0
    • RE: Documentation for WebSocket Feed

      @rajanprabu Thanks! Do share your experience.

      posted in Python SDK
      K
      Kavita 0
    • Maximum number of tokens for market feed

      Hi,

      For the market feed, we can get feed of multiple scrip IDs by the & parameter. Is there a limit on the number of tokens I can put? Can I, for example, have 2000 tokens in one query?

      More importanlty, would there be any performance issues with multiple tokens?

      Regards,

      posted in Python SDK
      K
      Kavita 0
    • Documentation for WebSocket Feed

      Hi,

      Could you please give more details on the web socket feed. Specifically, the task parameter.

      1. What is the difference between mw, sfi and and dp.
      2. What is the structure of the response in each of these fields

      Would be great if some forum members elaborate on this.

      Thanks!

      posted in Python SDK
      K
      Kavita 0