Skip to content

feat: live check for chart stock market app #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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