Skip to content

Commit

Permalink
bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
MuslemRahimi committed Dec 10, 2024
1 parent 6340389 commit 8fa8cff
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions fastify/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,16 @@ fastify.register(async function (fastify) {
const lastSentData = {};

// Function to send data for all tickers as a list
const sendData = async () => {
const sendData = async () => {
const dataToSend = [];

for (const symbol of tickers) {
const filePath = path?.join(
__dirname,
`../app/json/websocket/companies/${symbol?.toUpperCase()}.json`
);
try {

try {
if (fs?.existsSync(filePath)) {
const fileData = fs?.readFileSync(filePath, "utf8");

Expand All @@ -282,7 +283,7 @@ try {
jsonData?.ap != null &&
jsonData?.bp != null &&
jsonData?.t != null &&
["Q","T"]?.includes(jsonData?.type) &&
["Q", "T"]?.includes(jsonData?.type) &&
connection.socket.readyState === WebSocket.OPEN
) {
const avgPrice = (
Expand All @@ -295,21 +296,24 @@ try {
? jsonData.bp
: avgPrice;

const currentDataSignature = `${jsonData?.bp}`;
const lastSentSignature = lastSentData[symbol];

if (currentDataSignature !== lastSentSignature) {
dataToSend?.push({
symbol,
ap: jsonData?.ap,
bp: jsonData?.bp,
lp: jsonData?.lp,
avgPrice: finalPrice,
type: jsonData?.type,
time: formatTimestampNewYork(jsonData?.t),
});

lastSentData[symbol] = currentDataSignature;
// Check if finalPrice is equal to avgPrice before sending data
if (finalPrice - avgPrice === 0) {
const currentDataSignature = `${finalPrice}`;
const lastSentSignature = lastSentData[symbol];

if (currentDataSignature !== lastSentSignature) {
dataToSend?.push({
symbol,
ap: jsonData?.ap,
bp: jsonData?.bp,
lp: jsonData?.lp,
avgPrice: finalPrice,
type: jsonData?.type,
time: formatTimestampNewYork(jsonData?.t),
});

lastSentData[symbol] = currentDataSignature;
}
}
}
} else {
Expand All @@ -323,10 +327,11 @@ try {
// Send all collected data as a single message
if (dataToSend.length > 0 && connection.socket.readyState === WebSocket.OPEN) {
connection.socket.send(JSON.stringify(dataToSend));
//console.log(dataToSend);
// console.log(dataToSend);
}
};



// Start receiving messages from the client
connection.socket.on("message", (message) => {
Expand Down

0 comments on commit 8fa8cff

Please sign in to comment.