This project provides REST API and WebSocket endpoints for live financial instrument prices sourced from Binance. It supports efficiently handling over 1,000 subscribers. This implementation supports dynamic handling of multiple financial instruments.
- .NET 8.0 SDK
- A code editor (e.g., Visual Studio Code)
-
Clone the repository:
git clone https://github.com/khachatur/FinancialPriceService.git cd FinancialPriceService -
Restore the dependencies:
dotnet restore
-
Build the project:
dotnet build
-
Run the application:
dotnet run
-
Open the sample client:
Open
wwwroot/index.htmlin a web browser to see real-time price updates.
-
Get List of Instruments:
GET /api/prices/instrumentsReturns the list of available financial instruments.
Example response:
["BTCUSD", "ETHUSD", "EURUSD"]
-
Get Current Price:
GET /api/prices/currentprice/{instrument}Returns the current price of the specified financial instrument.
Example response:
{ "Instrument": "BTCUSD", "Price": 66315.15 }
- Subscribe to Live Price Updates:
Connect to ws://localhost:5273/ws using a WebSocket client to receive live updates for BTCUSD from Binance.
- Program.cs: Main entry point of the application, configures services, and sets up endpoints.
- WebSocketConnectionManager.cs: Manages WebSocket connections and broadcasting messages.
- PriceStore.cs: Stores and updates the latest prices.
- Controllers/PricesController.cs: Handles REST API requests for financial instrument prices.
- Services/BinanceWebSocketService.cs: Background service that connects to Binance WebSocket and updates prices.
- Hubs/PriceHub.cs: Defines the SignalR hub that clients can connect to for receiving real-time updates.
- wwwroot/: Contains static files for the project, including a sample client (index.html) to test SignalR connections.
The application logs important events and errors to the console. Ensure you check the console output for real-time logging information.
- The application subscribes to the Binance WebSocket stream for BTCUSD price updates. You can modify the code to subscribe to additional instruments if needed.
- The application is designed to handle over 1,000 WebSocket subscribers efficiently.
If you encounter any issues, please check the following:
- Ensure you have .NET 8.0 SDK installed and properly set up on your machine.
- Check the console logs for any error messages.
- Ensure you have a stable internet connection to connect to Binance WebSocket.