@extra-fyers/websocket.web
Version:
A Javascript interface for FYERS API {websocket} web.
1 lines • 4.75 kB
JavaScript
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).extra_fyers_websocket=e()}}((function(){return function e(t,r,n){function i(s,a){if(!r[s]){if(!t[s]){var c="function"==typeof require&&require;if(!a&&c)return c(s,!0);if(o)return o(s,!0);var u=new Error("Cannot find module '"+s+"'");throw u.code="MODULE_NOT_FOUND",u}var f=r[s]={exports:{}};t[s][0].call(f.exports,(function(e){return i(t[s][1][e]||e)}),f,f.exports,e,t,r,n)}return r[s].exports}for(var o="function"==typeof require&&require,s=0;s<n.length;s++)i(n[s]);return i}({1:[function(e,t,r){"use strict";var n=e("ws");const i="wss://api.fyers.in/socket/v2/dataSock",o="wss://api.fyers.in/socket/v2/orderSock";function s(e,t){return{price:e.getUint32(t+0),volume:e.getUint32(t+4),ord:e.getUint32(t+8)}}function a(e,t,r){!function(e,t,r){e.token=t.getBigUint64(r+0),e.tt=t.getUint32(r+8),e.fyCode=t.getUint16(r+12),e.marketStat=t.getUint16(r+14),e.pktlen=t.getUint16(r+16),e.L2=t.getUint8(r+18)}(e,t,r+0);var n=7202===e.fyCode||31038===e.fyCode,i=n?88:72;7202===e.fyCode?function(e,t,r){e.oi=BigInt(t.getUint32(r+0)),e.pdoi=BigInt(t.getUint32(r+4))}(e,t,r+24):function(e,t,r,n){e.price_conv=t.getUint32(r+0),e.ltp=t.getUint32(r+4),e.open_price=t.getUint32(r+8),e.high_price=t.getUint32(r+12),e.low_price=t.getUint32(r+16),e.prev_close_price=t.getUint32(r+20),e.o=t.getUint32(r+24),e.h=t.getUint32(r+28),e.l=t.getUint32(r+32),e.c=t.getUint32(r+36),e.v=t.getBigUint64(r+40),n&&(e.oi=t.getBigUint64(r+48),e.pdoi=t.getBigUint64(r+56))}(e,t,r+24,n),7208!==e.fyCode&&31038!==e.fyCode||function(e,t,r,n){e.LTQ=t.getUint32(r+0),e.L2_LTT=t.getUint32(r+4),e.ATP=t.getUint32(r+8),e.volume=t.getUint32(r+12),e.tot_buy=t.getBigUint64(r+16),e.tot_sell=t.getBigUint64(r+24),n&&(e.bid=t.getUint32(r+32),e.ask=t.getUint32(r+36))}(e,t,r+i,1!==e.L2),1===e.L2&&function(e,t,r){for(var n=[],i=[],o=0;o<60;o+=12)n.push(s(t,r+o)),i.push(s(t,r+o+60));e.bids=n,e.asks=i}(e,t,r+i+32)}function c(e,t){var r={token:BigInt(0),tt:0,fyCode:0,marketStat:0,pktlen:0,L2:0,price_conv:0,ltp:0,open_price:0,high_price:0,low_price:0,prev_close_price:0,o:0,h:0,l:0,c:0,v:BigInt(0),oi:BigInt(0),pdoi:BigInt(0),LTQ:0,L2_LTT:0,ATP:0,volume:0,tot_buy:BigInt(0),tot_sell:BigInt(0),bid:0,ask:0,bids:null,asks:null};return a(r,e,t),r}class u extends n.WebSocket{constructor(){super(...arguments),this.recievers=[]}}function f(e){return new Promise(((t,r)=>e.recievers.push({resolve:t,reject:r})))}r.Connection=u,r.MARKET_DATA_URL=i,r.ORDER_UPDATE_URL=o,r.connectMarketData=function(e,t){return new Promise(((r,o)=>{var{app_id:s,access_token:a}=e,f=new u(`${i}?${`user-agent=fyers-api&type=symbolUpdate&access_token=${s}:${a}`}`),d=setInterval((()=>{f.readyState===n.WebSocket.OPEN&&f.send("ping")}),5e3);f.binaryType="arraybuffer",f.onerror=e=>{for(var t of f.recievers)t.reject(e.error);clearInterval(d),o(e.error)},f.onopen=()=>r(f),f.onmessage=e=>{if("string"==typeof e.data){if("pong"===e.data)return;var r=JSON.parse(e.data);if(t(r),r.hasOwnProperty("d")||0===r.code)return;f.recievers.length>0&&f.recievers.shift().resolve(r)}else{var n=new DataView(e.data);t({s:"ok",d:c(n,0)})}}}))},r.connectOrderUpdate=function(e,t){return new Promise(((r,i)=>{var{app_id:s,access_token:a}=e,c=new u(`${o}?${`user-agent=fyers-api&type=orderUpdate&access_token=${s}:${a}`}`),f=setInterval((()=>{c.readyState===n.WebSocket.OPEN&&c.send("ping")}),5e3);c.binaryType="arraybuffer",c.onerror=e=>{for(var t of c.recievers)t.reject(e.error);clearInterval(f),i(e.error)},c.onopen=()=>r(c),c.onmessage=e=>{if("string"==typeof e.data&&"pong"!==e.data){var r=JSON.parse(e.data);t(r),r.hasOwnProperty("d")||0===r.code||c.recievers.length>0&&c.recievers.shift().resolve(r)}}}))},r.subscribeMarketDepth=function(e,t){var r={T:"SUB_L2",L2LIST:t,SUB_T:1};return e.send(JSON.stringify(r)),f(e)},r.subscribeMarketQuote=function(e,t){var r={T:"SUB_DATA",TLIST:t,SUB_T:1};return e.send(JSON.stringify(r)),f(e)},r.subscribeOrderUpdate=function(e){return e.send(JSON.stringify({T:"SUB_ORD",SLIST:["orderUpdate"],SUB_T:1})),f(e)},r.unsubscribeMarketDepth=function(e,t){var r={T:"SUB_L2",L2LIST:t,SUB_T:0};return e.send(JSON.stringify(r)),f(e)},r.unsubscribeMarketQuote=function(e,t){var r={T:"SUB_DATA",TLIST:t,SUB_T:0};return e.send(JSON.stringify(r)),f(e)},r.unsubscribeOrderUpdate=function(e){return e.send(JSON.stringify({T:"SUB_ORD",SLIST:["orderUpdate"],SUB_T:0})),f(e)}},{ws:2}],2:[function(e,t,r){"use strict";t.exports=function(){throw new Error("ws does not work in the browser. Browser clients must use the native WebSocket object")}},{}]},{},[1])(1)}));