fyers-api-v3 1.3.4 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HSM/datasocket.min.js +1 -1
- package/HSM/mapper.js +220 -220
- package/HSM_Package/hslib.js +4 -4
- package/README.md +171 -171
- package/apiService/apiService.js +1026 -995
- package/config/config.js +30 -29
- package/errorHandler/errorHandler.js +36 -36
- package/index.js +11 -9
- package/logger/log.js +61 -61
- package/obfuscate.js +24 -24
- package/ordersocket/fyersSocket.js +325 -325
- package/ordersocket/mapper.js +78 -78
- package/package.json +25 -24
- package/sample/api.js +45 -45
- package/sample/datasocket.js +24 -24
- package/sample/ordersocket.js +29 -29
- package/sample/tbtsocket.js +25 -0
- package/tbtsocket/models.js +172 -0
- package/tbtsocket/msg.js +5163 -0
- package/tbtsocket/tbtSocket.js +339 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
const WebSocket = require('ws');
|
|
2
|
+
let Logger = require("../logger/log.js")
|
|
3
|
+
let { Config } = require("../config/config.js");
|
|
4
|
+
let {DataStore, Depth, SubscriptionModes, SubscriptionInfo} = require('./models.js')
|
|
5
|
+
let {SocketMessage} = require('./msg.js')
|
|
6
|
+
const axios = require('axios');
|
|
7
|
+
var reconnectiontries = 0
|
|
8
|
+
|
|
9
|
+
function getUrl(accessToken) {
|
|
10
|
+
try {
|
|
11
|
+
const response = https.get('https://api-t1.fyers.in/indus/home/tbtws', {
|
|
12
|
+
headers: {
|
|
13
|
+
'Authorization': accessToken
|
|
14
|
+
}
|
|
15
|
+
}, (res) => {
|
|
16
|
+
let data = '';
|
|
17
|
+
res.on('data', chunk => {
|
|
18
|
+
data += chunk;
|
|
19
|
+
});
|
|
20
|
+
res.on('end', () => {
|
|
21
|
+
try {
|
|
22
|
+
const jsonData = JSON.parse(data);
|
|
23
|
+
return jsonData.data.socket_url;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error('Error parsing response:', error);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error('Error fetching URL:', error);
|
|
31
|
+
}
|
|
32
|
+
return "wss://rtsocket-api.fyers.in/versova";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Class to access order update websocket.
|
|
37
|
+
* @class
|
|
38
|
+
*/
|
|
39
|
+
class FyersTbtSocket {
|
|
40
|
+
|
|
41
|
+
constructor(authorizationKey, logpath = undefined ,loggingFlag=true, diffOnly=false) {
|
|
42
|
+
this.url = getUrl(authorizationKey);
|
|
43
|
+
this.authorizationKey = authorizationKey;
|
|
44
|
+
this.ws = null;
|
|
45
|
+
this.onErrorCallback = null;
|
|
46
|
+
this.LogPath = logpath;
|
|
47
|
+
this.Logger = new Logger(this.LogPath,loggingFlag);
|
|
48
|
+
this.isPingEnabled = true;
|
|
49
|
+
this.pingInterval = null;
|
|
50
|
+
this.autoreconnectinterval = null;
|
|
51
|
+
this.isUserClosed=false
|
|
52
|
+
this.autoreconnectflag=false
|
|
53
|
+
this.maxreconnectiontries = 0
|
|
54
|
+
this.datastore = new DataStore()
|
|
55
|
+
this.subsinfo = new SubscriptionInfo()
|
|
56
|
+
this.onDepthCallback = null;
|
|
57
|
+
this.onErrorMsgCallback = null;
|
|
58
|
+
this.diffOnly = diffOnly
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* called to connect to the order socket
|
|
63
|
+
*/
|
|
64
|
+
connect() {
|
|
65
|
+
const logger = this.Logger
|
|
66
|
+
try {
|
|
67
|
+
logger.debug("initalizing connection to socket", { "accesstoken": this.authorizationKey })
|
|
68
|
+
this.ws = new WebSocket(this.url, {
|
|
69
|
+
headers: {
|
|
70
|
+
Authorization: this.authorizationKey,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
this.ws.binaryType = 'arraybuffer';
|
|
74
|
+
this.ws.on('error', (error) => {
|
|
75
|
+
if (this.onErrorCallback) {
|
|
76
|
+
this.onErrorCallback(error);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
console.log("error occoured", error)
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
this.ws.on('close', (event) => {
|
|
84
|
+
this.stopPing()
|
|
85
|
+
if (this.onCloseCallback) {
|
|
86
|
+
this.onCloseCallback(event);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
console.log("ws closed", event)
|
|
90
|
+
if(!this.isUserClosed&&(reconnectiontries<this.maxreconnectiontries)){
|
|
91
|
+
this._autoreconnect()
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
this.ws.on('message', (message) => {
|
|
97
|
+
const data = message.toString();
|
|
98
|
+
if (data === 'pong') {
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const m = SocketMessage.decode(Buffer.from(message));
|
|
103
|
+
if (m.error) {
|
|
104
|
+
if (this.onErrorMsgCallback) {
|
|
105
|
+
this.onErrorMsgCallback(m.msg);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
console.log("error occoured", m.error)
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
this.datastore.updateDepth(m, this.onDepthCallback, this.diffOnly)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
this.ws.on('open', () => {
|
|
117
|
+
reconnectiontries = 0
|
|
118
|
+
if (this.onOpenCallback) {
|
|
119
|
+
this.onOpenCallback();
|
|
120
|
+
let open_chans = this.subsinfo.getChannelInfo()
|
|
121
|
+
this.switchChannel(new Set(), open_chans)
|
|
122
|
+
for (var c in open_chans) {
|
|
123
|
+
let symbols = this.subsinfo.getSymbolsInfo(c)
|
|
124
|
+
let mode = this.subsinfo.getModeInfo(c)
|
|
125
|
+
this.subscribe(symbols, c, mode)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
console.log("connected")
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (this.isPingEnabled) {
|
|
133
|
+
this.startPing();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
console.log(`Error occured: ${error}`)
|
|
139
|
+
logger.error("unexpected error on connect function", error)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* used to define onmessage,onerror,onopen,onclose for websocket.
|
|
145
|
+
* @param {string} onwhat - defines for what the callback function is.
|
|
146
|
+
* @param {Function} callback - the callback function.
|
|
147
|
+
* @throws error message if onwhat is not valid.
|
|
148
|
+
*/
|
|
149
|
+
on(onwhat, callback) {
|
|
150
|
+
const funcname = "on"
|
|
151
|
+
const logger = this.Logger
|
|
152
|
+
try {
|
|
153
|
+
if (onwhat === 'error') {
|
|
154
|
+
this.onErrorCallback = callback;
|
|
155
|
+
}
|
|
156
|
+
else if (onwhat === 'depth') {
|
|
157
|
+
this.onDepthCallback = callback;
|
|
158
|
+
}
|
|
159
|
+
else if (onwhat === 'servererror') {
|
|
160
|
+
this.onErrorMsgCallback = callback;
|
|
161
|
+
}
|
|
162
|
+
else if (onwhat === 'close') {
|
|
163
|
+
const socketonclose = function (event){
|
|
164
|
+
callback(event)
|
|
165
|
+
if(!this.isUserClosed&&(reconnectiontries<this.maxreconnectiontries)){
|
|
166
|
+
this._autoreconnect()
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
this.onCloseCallback = socketonclose;
|
|
170
|
+
}
|
|
171
|
+
else if (onwhat === 'open') {
|
|
172
|
+
this.onOpenCallback = callback;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
console.log("incorrect value passed", onwhat, "allowed values are: error, depth, servererror, close and open")
|
|
176
|
+
logger.error("wrong onwhat passed", { "onwhat": onwhat }, funcname)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
logger.error("unexpected error on 'on' function", error, funcname)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* call to check if datasocket is connected
|
|
186
|
+
*/
|
|
187
|
+
isConnected() {
|
|
188
|
+
return this.ws && this.ws.readyState === WebSocket.OPEN;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* starts ping messages to ws
|
|
193
|
+
*/
|
|
194
|
+
startPing() {
|
|
195
|
+
this.pingInterval = setInterval(() => {
|
|
196
|
+
if (this.isConnected()) {
|
|
197
|
+
this.ws.send("ping");
|
|
198
|
+
}
|
|
199
|
+
}, 1000);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* stops pinging mechanism
|
|
204
|
+
*/
|
|
205
|
+
stopPing() {
|
|
206
|
+
clearInterval(this.pingInterval);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Subscribe to socket.
|
|
211
|
+
* @param {Array|string} towhat - array or string of what you want to subscribe to.
|
|
212
|
+
*/
|
|
213
|
+
subscribe(symbolTickers, channelNo, mode) {
|
|
214
|
+
const logger = this.Logger
|
|
215
|
+
try {
|
|
216
|
+
if (this.isConnected()) {
|
|
217
|
+
|
|
218
|
+
this.subsinfo.subscribe(symbolTickers, channelNo, mode);
|
|
219
|
+
this.ws.send(JSON.stringify({
|
|
220
|
+
"type": 1,
|
|
221
|
+
"data": {
|
|
222
|
+
"subs": 1,
|
|
223
|
+
"symbols": Array.from(symbolTickers), // Convert Set to array
|
|
224
|
+
"mode": mode,
|
|
225
|
+
"channel": channelNo
|
|
226
|
+
}
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
logger.error("websocket is not connected", { "towhat": towhat },)
|
|
231
|
+
console.log('Cannot send message. WebSocket is not connected.');
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
|
|
236
|
+
logger.error("unexpected error on subscribe function", error,)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Unsubscribe to socket.
|
|
242
|
+
* @param {Array|string} towhat - array or string of what you want to unsubscribe to.
|
|
243
|
+
*/
|
|
244
|
+
unsubscribe(symbolTickers, channelNo, mode) {
|
|
245
|
+
const logger = this.Logger
|
|
246
|
+
try {
|
|
247
|
+
if (this.isConnected()) {
|
|
248
|
+
this.subsinfo.subscribe(symbolTickers, channelNo, mode);
|
|
249
|
+
this.ws.send(JSON.stringify({
|
|
250
|
+
"type": 1,
|
|
251
|
+
"data": {
|
|
252
|
+
"subs": -1,
|
|
253
|
+
"symbols": Array.from(symbolTickers), // Convert Set to array
|
|
254
|
+
"mode": mode,
|
|
255
|
+
"channel": channelNo
|
|
256
|
+
}
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
logger.error("websocket is not connected", { "towhat": towhat }, )
|
|
261
|
+
console.log('Cannot send message. WebSocket is not connected.');
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
|
|
266
|
+
logger.error("unexpected error on unsubscribe function", error, )
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
switchChannel(pausedChannels, resumedChannels) {
|
|
271
|
+
const logger = this.Logger
|
|
272
|
+
try {
|
|
273
|
+
if (this.isConnected()) {
|
|
274
|
+
this.subsinfo.updateChannels(pausedChannels, resumedChannels);
|
|
275
|
+
this.ws.send(JSON.stringify(
|
|
276
|
+
{
|
|
277
|
+
"type": 2,
|
|
278
|
+
"data": {
|
|
279
|
+
"resumeChannels": Array.from(resumedChannels),
|
|
280
|
+
"pauseChannels": Array.from(pausedChannels)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
));
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
logger.error("websocket is not connected", { "towhat": towhat }, )
|
|
287
|
+
console.log('Cannot send message. WebSocket is not connected.');
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
|
|
292
|
+
logger.error("unexpected error on switchchannel function", error)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* call to close the datasocket.
|
|
298
|
+
*/
|
|
299
|
+
close() {
|
|
300
|
+
if (this.ws && this.isConnected) {
|
|
301
|
+
clearInterval(this.autoreconnectinterval)
|
|
302
|
+
this.stopPing()
|
|
303
|
+
this.ws.close();
|
|
304
|
+
this.isUserClosed = true
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* call to enable autoreconnect functionality of websocket.
|
|
310
|
+
*/
|
|
311
|
+
_autoreconnect() {
|
|
312
|
+
if (this.autoreconnectflag){
|
|
313
|
+
var waitSeconds = Math.floor((reconnectiontries+5)/5);
|
|
314
|
+
waitSeconds *= 5;
|
|
315
|
+
|
|
316
|
+
this.autoreconnectinterval = setTimeout(() => {
|
|
317
|
+
if ((!this.isConnected()) && (reconnectiontries < this.maxreconnectiontries)) {
|
|
318
|
+
console.log("trying to reconnect ", reconnectiontries + 1)
|
|
319
|
+
reconnectiontries++
|
|
320
|
+
this.connect()
|
|
321
|
+
} else if (reconnectiontries >= this.maxreconnectiontries) {
|
|
322
|
+
console.log("max autoconnect tries exceeded")
|
|
323
|
+
clearInterval(this.autoreconnectinterval)
|
|
324
|
+
this.stopPing()
|
|
325
|
+
reconnectiontries = 0
|
|
326
|
+
}
|
|
327
|
+
}, (waitSeconds)*1000);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* call to enable autoreconnect functionality of websocket.
|
|
332
|
+
*/
|
|
333
|
+
autoreconnect(reConnectTriesCount=5){
|
|
334
|
+
this.autoreconnectflag=true
|
|
335
|
+
this.maxreconnectiontries=(reConnectTriesCount>50) ? 50 : reConnectTriesCount
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
module.exports = FyersTbtSocket
|