Skip to content

Commit

Permalink
feat: Live check for chart stock market app (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
a2937 committed Nov 15, 2023
1 parent 6713127 commit 68fe938
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions apps/chart-the-stock-market/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ require('dotenv').config();
const app = express();
app.use(express.static('public'));

app.get('/status/ping', (req, res) => {
res.status(200).send({ msg: 'pong' });
});

const portNum = process.env.PORT || 3000;

const server = app.listen(portNum, () => {
Expand All @@ -20,13 +24,16 @@ function emitStockData() {
io.emit('updateStockData', JSON.stringify(currentStockData));
}

io.on('connection', function(socket) {
io.on('connection', function (socket) {
emitStockData();

socket.on('newStock', function(stock) {
if(!(stock.symbol in currentStockData) && Object.keys(currentStockData).length < 4) {
socket.on('newStock', function (stock) {
if (
!(stock.symbol in currentStockData) &&
Object.keys(currentStockData).length < 4
) {
const requestUrl = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${stock.symbol}&apikey=${process.env.APIKEY}`;

https.get(requestUrl, res => {
let stockData = '';
res.setEncoding('utf8');
Expand All @@ -38,14 +45,16 @@ io.on('connection', function(socket) {
res.on('end', () => {
stockData = JSON.parse(stockData);

if (stockData["Error Message"]) { //stock symbol requested doesn't exist
if (stockData['Error Message']) {
//stock symbol requested doesn't exist
io.emit('stopLoading');
} else { //stock symbol requested found
currentStockData[stock.symbol] = stockData['Time Series (Daily)']
} else {
//stock symbol requested found
currentStockData[stock.symbol] = stockData['Time Series (Daily)'];
emitStockData();
}

res.on('error', (e) => {
res.on('error', e => {
console.log('error fetching stock data');
console.error(e);
io.emit('stopLoading');
Expand All @@ -57,10 +66,10 @@ io.on('connection', function(socket) {
}
});

socket.on('deleteStock', function(stock) {
socket.on('deleteStock', function (stock) {
console.log('delete' + stock);
console.log(stock.symbol);
if(stock.symbol in currentStockData) {
if (stock.symbol in currentStockData) {
delete currentStockData[stock.symbol];

emitStockData();
Expand Down

0 comments on commit 68fe938

Please sign in to comment.