This is a simple web application that shows live stock prices using Server-Sent Events (SSE) and real-time stock data fetched from external APIs (e.g., Twelve Data or Finnhub).
- Real-time stock price updates (e.g., Apple Inc)
- Uses SSE (Server-Sent Events) for pushing data from backend to frontend
- Built using Node.js, Express, Axios, and vanilla HTML/CSS/JS
- API data source can be switched easily (e.g., from Twelve Data to Finnhub)
- Minimal, easy-to-understand project for learning SSE & real-time data fetching
- Frontend: HTML, JavaScript
- Backend: Node.js, Express
- API: Twelve Data API
├── server.js # Express server (SSE + API fetch) ├── .env # API keys and port config ├── public/ │ └── index.html # Frontend HTML for displaying live prices └── README.md
bash git clone https://github.com/your-username/stock-price-sse.git cd stock-price-sse
bash npm install
Create a .env
file at the root with the following content:
env API_KEY=your_api_key_here PORT=5000
🔑 You can get a free API key from:
bash node server.js
Use Live Server (VS Code extension) or any static file server to open index.html
:
Then open http://localhost:5500
in your browser.
If the real API doesn't provide frequent updates (e.g., free tier of Twelve Data), you can test with random mock data instead:
js const price = (150 + Math.random() * 10).toFixed(2); res.write(`data: ${JSON.stringify({ symbol: 'AAPL', price })}
`);
If you see a CORS error, make sure:
- The backend uses correct
cors()
settings. - The frontend and backend run on the same origin (use
localhost
consistently). - Example: js app.use(cors({ origin: 'http://localhost:5500', credentials: true }));
- Learning and demoing Server-Sent Events
- Building simple dashboards
- Real-time data stream visualization
Contributions are welcome! Feel free to fork the repo and submit a PR.
MIT License. See LICENSE
file for details.