UNPKG

@extra-fyers/websocket.web

Version:

A Javascript interface for FYERS API {websocket} web.

1 lines 4.42 kB
import{WebSocket}from"ws";const MARKET_DATA_URL="wss://api.fyers.in/socket/v2/dataSock",ORDER_UPDATE_URL="wss://api.fyers.in/socket/v2/orderSock",MARKET_DATA_QUERY="user-agent=fyers-api&type=symbolUpdate",ORDER_UPDATE_QUERY="user-agent=fyers-api&type=orderUpdate",PING_INTERVAL=5e3;function writeHeader(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)}function writeOiData(e,t,r){e.oi=BigInt(t.getUint32(r+0)),e.pdoi=BigInt(t.getUint32(r+4))}function writeCommonData(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))}function writeL1Data(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))}function toL2MarketOffer(e,t){return{price:e.getUint32(t+0),volume:e.getUint32(t+4),ord:e.getUint32(t+8)}}function writeL2Data(e,t,r){for(var n=[],i=[],a=0;a<60;a+=12)n.push(toL2MarketOffer(t,r+a)),i.push(toL2MarketOffer(t,r+a+60));e.bids=n,e.asks=i}function writeMarketData(e,t,r){writeHeader(e,t,r+0);var n=7202===e.fyCode||31038===e.fyCode,i=n?88:72;7202===e.fyCode?writeOiData(e,t,r+24):writeCommonData(e,t,r+24,n),7208!==e.fyCode&&31038!==e.fyCode||writeL1Data(e,t,r+i,1!==e.L2),1===e.L2&&writeL2Data(e,t,r+i+32)}function toMarketData(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 writeMarketData(r,e,t),r}class Connection extends WebSocket{constructor(){super(...arguments),this.recievers=[]}}function waitResponse(e){return new Promise(((t,r)=>e.recievers.push({resolve:t,reject:r})))}function connectOrderUpdate(e,t){return new Promise(((r,n)=>{var{app_id:i,access_token:a}=e,o=new Connection(`${ORDER_UPDATE_URL}?${`${ORDER_UPDATE_QUERY}&access_token=${i}:${a}`}`),s=setInterval((()=>{o.readyState===WebSocket.OPEN&&o.send("ping")}),5e3);o.binaryType="arraybuffer",o.onerror=e=>{for(var t of o.recievers)t.reject(e.error);clearInterval(s),n(e.error)},o.onopen=()=>r(o),o.onmessage=e=>{if("string"==typeof e.data&&"pong"!==e.data){var r=JSON.parse(e.data);t(r),r.hasOwnProperty("d")||0===r.code||o.recievers.length>0&&o.recievers.shift().resolve(r)}}}))}function subscribeOrderUpdate(e){return e.send(JSON.stringify({T:"SUB_ORD",SLIST:["orderUpdate"],SUB_T:1})),waitResponse(e)}function unsubscribeOrderUpdate(e){return e.send(JSON.stringify({T:"SUB_ORD",SLIST:["orderUpdate"],SUB_T:0})),waitResponse(e)}function connectMarketData(e,t){return new Promise(((r,n)=>{var{app_id:i,access_token:a}=e,o=new Connection(`${MARKET_DATA_URL}?${`${MARKET_DATA_QUERY}&access_token=${i}:${a}`}`),s=setInterval((()=>{o.readyState===WebSocket.OPEN&&o.send("ping")}),5e3);o.binaryType="arraybuffer",o.onerror=e=>{for(var t of o.recievers)t.reject(e.error);clearInterval(s),n(e.error)},o.onopen=()=>r(o),o.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;o.recievers.length>0&&o.recievers.shift().resolve(r)}else{var n=new DataView(e.data);t({s:"ok",d:toMarketData(n,0)})}}}))}function subscribeMarketQuote(e,t){var r={T:"SUB_DATA",TLIST:t,SUB_T:1};return e.send(JSON.stringify(r)),waitResponse(e)}function subscribeMarketDepth(e,t){var r={T:"SUB_L2",L2LIST:t,SUB_T:1};return e.send(JSON.stringify(r)),waitResponse(e)}function unsubscribeMarketQuote(e,t){var r={T:"SUB_DATA",TLIST:t,SUB_T:0};return e.send(JSON.stringify(r)),waitResponse(e)}function unsubscribeMarketDepth(e,t){var r={T:"SUB_L2",L2LIST:t,SUB_T:0};return e.send(JSON.stringify(r)),waitResponse(e)}export{Connection,MARKET_DATA_URL,ORDER_UPDATE_URL,connectMarketData,connectOrderUpdate,subscribeMarketDepth,subscribeMarketQuote,subscribeOrderUpdate,unsubscribeMarketDepth,unsubscribeMarketQuote,unsubscribeOrderUpdate};