In this lab activity, you will implement a real-time stock market dashboard using FastAPI and WebSockets. The dashboard will display stock price changes over time using a dynamic chart and price cards that update in real-time.
Previously, we've used standard HTTP requests to communicate between the client and server. Imagine if we had to just use what we've learned so far to implement a real-time stock market dashboard. We would have to continuously poll the server for new stock prices using GET requests. This approach is inefficient and not scalable!
When it comes to real-time applications where you need data to be rapidly updated, WebSockets are a better choice. WebSockets provide a two-way communication channel between the client and server, allowing for real-time data exchange.
The lab session slides explain the concept of WebSockets in more detail, and we also encourage you to read though these resources:
Similar to what we did in previous labs, do the following:
- Build and run the container:
docker-compose up --build
- Open
http://localhost:8000in your web browser.
Of course, the dashboard will not display any real-time data yet. You will need to implement the WebSocket connection between the client and server to achieve this.
This lab contains TODOs in main.py and index.html. Complete the following tasks to fully implement the real-time stock market visualization.
If you implemented everything successfully, you should see:
- The stock chart updating in real-time with new datapoints every second.
- The stock cards updating in real-time with new stock prices and the percentage change.
File: app/index.html
- Locate the
// TODO: Initialize the WebSocket connectionsection. - Create a WebSocket object that connects to
ws://localhost:8000/ws.- We use
/wsbecause that's the endpoint we defined in the FastAPI server inmain.py.
- We use
- Handle incoming messages to update the chart and stock cards.
File: app/index.html
- Locate the
// TODO: Track previous prices for calculating changessection. - Store the last received prices in a dictionary to compute percentage changes.
File: app/index.html
- Locate the
// TODO: Implement the message handlersection. - When new stock data arrives:
- Update the chart with
updateChartData(data). - Update the stock cards using
updatePriceCards(data, previousPrices). - Store the new prices in
previousPrices.
- Update the chart with
Hint: The onmessage event handler is used to handle incoming messages from the WebSocket server. Here is a template to get you started:
// Note that 'socket' is the WebSocket object you created in Task 1.
// Make sure to use the same variable name!
socket.onmessage = function(event) {
// add your code here
};File: app/index.html
- Locate the
// TODO: Implement error handlingsection. - Log errors to the console when they occur.
Hint: Similar to the onmessage event handler, you can use the onerror event handler to handle WebSocket errors.
File: app/index.html
- Locate the
// TODO: Implement connection close handlingsection. - Log when the WebSocket connection is closed.
Hint: We just implemented onmessage and onerror. You can probably guess what the event handler for connection closure is called.
File: app/main.py
- Locate the websocket endpoint:
@app.websocket("/ws"). - Complete the code to:
- Accept the WebSocket connection.
- Continuously send new stock prices every second using
get_new_stock_prices(). - Note that the
get_new_stock_prices()function is already implemented for you and returns the data as a dictionary. - Also note that you are asked to get the code to sleep for 1 second between sending new stock prices. Even though you've used
time.sleep()before, you should not use it here because this is an asynchronous function. Instead, you'll need to figure out how to do the equivalent using theasynciolibrary.
As directed on Gradescope submission, submit your index.html and main.py files.