Skip to content

Commit

Permalink
w/ testData, testOrder (websocket)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfram77 committed Apr 4, 2022
1 parent 06a7121 commit 76c48af
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 115 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extra-fyers",
"version": "1.1.2",
"version": "1.1.3",
"description": "A Javascript interface for FYERS API.",
"main": "index.js",
"module": "index.mjs",
Expand Down
112 changes: 0 additions & 112 deletions test.mjs

This file was deleted.

56 changes: 56 additions & 0 deletions testData.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fs from "fs";
import WebSocket from "ws";

const app_id = fs.readFileSync("app_id.log", "utf8").trim();
const access_token = fs.readFileSync("access_token.log", "utf8").trim();
const DATA_URL = "wss://api.fyers.in/socket/v2/dataSock";
const DATA_QUERY = "user-agent=fyers-api&type=symbolUpdate";
const REQUEST_L1 = {"T": "SUB_DATA", "TLIST": ["NSE:SBIN-EQ", "NSE:FCONSUMER-EQ"], "SUB_T": 1};
const REQUEST_L2 = {"T": "SUB_L2", "L2LIST": ["NSE:SBIN-EQ", "NSE:FCONSUMER-EQ"], "SUB_T": 1};




function mainDataUsingOn() {
var url = `${DATA_URL}?${DATA_QUERY}&access_token=${app_id}:${access_token}`;
var ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.on('error', (err) => {
console.error("Error:", err);
});
ws.on('close', (code, reason) => {
console.log("Closed:", code, reason);
});
ws.on('open', () => {
console.log("Opened:");
ws.send(JSON.stringify(REQUEST_L2));
setInterval(() => ws.send(JSON.stringify('ping')), 5000);
});
ws.on('message', (data, isBinary) => {
if (!isBinary) console.log('Message:', data.toString());
else console.log('Binary message:', data.byteLength);
});
}


function mainDataUsingProperty() {
var url = `${DATA_URL}?${DATA_QUERY}&access_token=${app_id}:${access_token}`;
var ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.onerror = (err) => {
console.error("Error:", err);
};
ws.onclose = (code, reason) => {
console.log("Closed:", code, reason);
};
ws.onopen = () => {
console.log("Opened.");
ws.send(JSON.stringify(REQUEST_L2));
setInterval(() => ws.send(JSON.stringify('ping')), 5000);
};
ws.onmessage = (e) => {
if (typeof e.data==='string') console.log('Message:', e.data);
else console.log('Binary message:', new DataView(e.data).byteLength);
};
}
mainDataUsingProperty();
55 changes: 55 additions & 0 deletions testOrder.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fs from "fs";
import WebSocket from "ws";

const app_id = fs.readFileSync("app_id.log", "utf8").trim();
const access_token = fs.readFileSync("access_token.log", "utf8").trim();
const ORDER_URL = "wss://api.fyers.in/socket/v2/orderSock";
const ORDER_QUERY = "user-agent=fyers-api&type=orderUpdate";
const REQUEST = {"T": "SUB_ORD", "SLIST": ["orderUpdate"], "SUB_T": 1};




function mainOrderUsingOn() {
var url = `${ORDER_URL}?${ORDER_QUERY}&access_token=${app_id}:${access_token}`;
var ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.on('error', (err) => {
console.error("Error:", err);
});
ws.on('close', (code, reason) => {
console.log("Closed:", code, reason);
});
ws.on('open', () => {
console.log("Opened:");
ws.send(JSON.stringify(REQUEST));
setInterval(() => ws.send(JSON.stringify('ping')), 5000);
});
ws.on('message', (data, isBinary) => {
if (!isBinary) console.log('Message:', data.toString());
else console.log('Binary message:', data.byteLength);
});
}


function mainOrderUsingProperty() {
var url = `${ORDER_URL}?${ORDER_QUERY}&access_token=${app_id}:${access_token}`;
var ws = new WebSocket(url);
ws.binaryType = 'arraybuffer';
ws.onerror = (err) => {
console.error("Error:", err);
};
ws.onclose = (code, reason) => {
console.log("Closed:", code, reason);
};
ws.onopen = () => {
console.log("Opened.");
ws.send(JSON.stringify(REQUEST));
setInterval(() => ws.send(JSON.stringify('ping')), 5000);
};
ws.onmessage = (e) => {
if (typeof e.data==='string') console.log('Message:', e.data);
else console.log('Binary message:', new DataView(e.data).byteLength);
};
}
mainOrderUsingProperty();

0 comments on commit 76c48af

Please sign in to comment.