☰
Login
Signup
Home
›
Node JS client
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Register
Categories
Recent Discussions
Activity
Categories
13.8K
All Categories
0
Incidents
153
Node JS client
40
Go client
793
.Net API client
378
Kite Publisher
537
.Net / VBA / Excel (3rd party)
457
Algorithms and Strategies
993
Java client
1.1K
API clients
404
PHP client
4K
Python client
346
Mobile and Desktop apps
1.4K
Market data (WebSockets)
3.3K
General
Click -> web socket get live price
mitd123
May 22
edited May 22
in
Node JS client
const KiteConnect = require('kiteconnect').KiteConnect;
import { KiteConnect } from "kiteconnect";
import { WebSocket } from "ws";
const { ObjectId } = require("mongodb");
const apiKey = Your Key;
const accessToken = Your accessToken;
const kite = new KiteConnect({ api_key: apiKey });
kite.setAccessToken(accessToken);
export const liveSocketPrice = async () => {
try {
const ws = new WebSocket(`wss://ws.kite.trade?api_key=${apiKey}&access_token=${accessToken}`);
ws.on("open", () => {
console.log("Connected to Kite WebSocket API");
const message = { a: "subscribe", v: [408065, 884737] };// enter witch stock price get
ws.send(JSON.stringify(message));
});
ws.on("message", (data) => {
if (data instanceof Buffer) {
let offset = 0;
while (offset < data.length) {
if (offset + 2 > data.length) break;
const packetCount = data.readUInt16BE(offset);
offset += 2;
for (let i = 0; i < packetCount; i++) {
if (offset + 2 > data.length) break;
const packetLength = data.readUInt16BE(offset);
offset += 2;
if (offset + packetLength > data.length) break;
const packet = data.slice(offset, offset + packetLength);
offset += packetLength;
if (packetLength >= 8) {
const instrumentToken = packet.readInt32BE(0);
const lastTradedPrice = packet.readInt32BE(4) / 100; // Prices are in paise, convert to rupees
console.log(`Instrument Token: ${instrumentToken}, Last Traded Price: ${lastTradedPrice.toFixed(2)}`);
if (packetLength > 8) {
const lastTradedQuantity = packet.readInt32BE(8);
const averageTradedPrice = packet.readInt32BE(12) / 100;
const volumeTraded = packet.readInt32BE(16);
const totalBuyQuantity = packet.readInt32BE(20);
const totalSellQuantity = packet.readInt32BE(24);
const openPrice = packet.readInt32BE(28) / 100;
const highPrice = packet.readInt32BE(32) / 100;
const lowPrice = packet.readInt32BE(36) / 100;
const closePrice = packet.readInt32BE(40) / 100;
// console.log(`Last Traded Quantity: ${lastTradedQuantity}, Average Traded Price: ${averageTradedPrice.toFixed(2)}`);
// console.log(`Volume Traded: ${volumeTraded}, Total Buy Quantity: ${totalBuyQuantity}, Total Sell Quantity: ${totalSellQuantity}`);
// console.log(`Open Price: ${openPrice.toFixed(2)}, High Price: ${highPrice.toFixed(2)}, Low Price: ${lowPrice.toFixed(2)}, Close Price: ${closePrice.toFixed(2)}`);
}
} else if (packetLength === 1) {
console.log('Received heartbeat or keep-alive message');
} else {
console.log(`Received binary data with unexpected length: ${packetLength}`);
}
}
}
} else if (typeof data === 'string') {
try {
const jsonData = JSON.parse(data);
console.log('Received JSON data:', jsonData);
} catch (e) {
console.log('Received non-JSON data:', data);
}
}
});
ws.on('close', () => {
console.log('Disconnected from Kite WebSocket API');
});
ws.on('error', (error) => {
console.error('WebSocket error:', error.message);
});
} catch (error) {
console.error('Error:', error.message);
}
}
Tagged:
web socket
Sign In
or
Register
to comment.