diff --git a/package-lock.json b/package-lock.json index a3d08ac..281d118 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "aws4": "^1.12.0", "axios": "^1.4.0", "cors": "^2.8.5", + "dotenv": "^16.4.5", "eslint": "8.42.0", "eslint-config-next": "13.4.4", "firebase": "^9.22.2", @@ -9271,6 +9272,17 @@ "csstype": "^3.0.2" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/duplexer": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz", diff --git a/package.json b/package.json index 162003f..4e35b91 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,6 @@ "@aws-sdk/credential-providers": "^3.515.0", "@emotion/react": "^11.11.1", "@emotion/styled": "^11.11.0", - - "@mui/icons-material": "^5.11.16", "@mui/material": "^5.13.5", "@react-oauth/google": "^0.11.0", @@ -31,6 +29,7 @@ "aws4": "^1.12.0", "axios": "^1.4.0", "cors": "^2.8.5", + "dotenv": "^16.4.5", "eslint": "8.42.0", "eslint-config-next": "13.4.4", "firebase": "^9.22.2", diff --git a/src/app/lib/mongodb.ts b/src/app/lib/mongodb.ts index ac4eac4..3b532db 100644 --- a/src/app/lib/mongodb.ts +++ b/src/app/lib/mongodb.ts @@ -1,10 +1,12 @@ import { MongoClient } from 'mongodb'; import { aggregateDailyData, ChartData } from '../utils/dataAggregation'; +import dotenv from 'dotenv'; + +dotenv.config(); // Load environment variables from .env file export async function fetchHistoricalData(specificStrikePrice: number, startDate: Date, endDate: Date) { - const uri = "mongodb://127.0.0.1:27017/"; + const uri = process.env.MONGODB_URI || "mongodb://127.0.0.1:27017/"; // Use MongoDB URI from environment variable or fallback to default - //const uri = "mongodb://ns3151328.ip-151-106-34.eu:27017/"; const client = new MongoClient(uri); try { @@ -38,4 +40,4 @@ export async function fetchHistoricalData(specificStrikePrice: number, startDate } finally { client.close(); } -} \ No newline at end of file +} diff --git a/src/hooks/useSocketMobx.ts b/src/hooks/useSocketMobx.ts index d1d9bcf..d5ad744 100644 --- a/src/hooks/useSocketMobx.ts +++ b/src/hooks/useSocketMobx.ts @@ -3,15 +3,13 @@ import { paytmSocketStore } from '../stores/PaytmSocketStore'; import { OptionData } from '../types'; import { autorun } from "mobx"; - export const useWebSocketMobX = () => { console.log("useWebSocketMobX called"); const [isInitialLoadCompleted, setInitialLoadCompleted] = useState(false); const socket = useRef(null); const accumulatedData = useRef([]); - const url = 'ws://tradepodsocket-vzpocpxkaa-uc.a.run.app/tradepod'; - //const url = 'ws://127.0.0.1:8888/tradepod'; + const url = process.env.NEXT_PUBLIC_WEBSOCKET_PROD || "wss://tradepodsocket-vzpocpxkaa-uc.a.run.app/tradepod"; // Track whether a response has been received const responseReceived = useRef(false); @@ -52,21 +50,19 @@ export const useWebSocketMobX = () => { } else if (lastStrike && dataObject.strikePrice !== lastStrike) { paytmSocketStore.updateData(dataObject); } - // Set the response received flag to true - responseReceived.current = true; + + // Set the response received flag to true + responseReceived.current = true; }; - const handleError = (error: Event) => { console.error("WebSocket Error:", error); responseReceived.current = true; - }; const handleClose = (event: CloseEvent) => { console.log("WebSocket Disconnected with code:", event.code, "reason:", event.reason); responseReceived.current = true; - }; socket.current.onopen = handleOpen; @@ -93,19 +89,19 @@ export const useWebSocketMobX = () => { }; }, []); - // Retry logic after waiting for a certain duration - useEffect(() => { - const retryTimeout = setTimeout(() => { - // Retry only if no response has been received - if (!responseReceived.current) { - console.log("No response received. Retrying..."); - connectWebSocket(); - } - }, 3000); // Wait for 3 seconds before retrying - - // Cleanup function - return () => clearTimeout(retryTimeout); - }, [responseReceived.current]); + // Retry logic after waiting for a certain duration + useEffect(() => { + const retryTimeout = setTimeout(() => { + // Retry only if no response has been received + if (!responseReceived.current) { + console.log("No response received. Retrying..."); + connectWebSocket(); + } + }, 3000); // Wait for 3 seconds before retrying + + // Cleanup function + return () => clearTimeout(retryTimeout); + }, [responseReceived.current]); useEffect(() => { // Re-establish WebSocket connection when selectedSymbol changes