Financial Streamer is a service that provides REST API and WebSocket endpoints for live financial instrument prices sourced from a public data provider. It is designed to efficiently handle over 1,000 subscribers.
- Endpoint to get a list of available financial instruments: Implemented in
PricesController.cs
with theGetInstruments
method. - Endpoint to get the current price of a specific financial instrument: Implemented in
PricesController.cs
with theGetPrice
method.
- Subscribe to live price updates for a specific financial instrument(s): Implemented in
WebSocketHandler.cs
with theHandleWebSocketAsync
,HandleMessageAsync
, andAddSubscriberAsync
methods. - Broadcast price updates to all subscribed clients: Implemented in
WebSocketHandler.cs
with theBroadcastPriceUpdateAsync
method.
- Use Tiingo API for live price data: Implemented in
TiingoPriceDataProvider.cs
using theRestless.Tiingo.Client
andRestless.Tiingo.Socket.Client
libraries.
- Efficiently manage 1,000+ WebSocket subscribers: The code uses
ConcurrentDictionary
to manage subscribers and ensures only one subscription per symbol to the data provider, optimizing resource usage. Detailed comments are included in the code to explain how high traffic is handled.
- Implement event and error logging capabilities: Logging is implemented throughout the code using
ILogger
. AnErrorHandlingMiddleware
is added to catch and log unhandled exceptions, providing a consistent error response format.
- FinancialStreamer.Api: Contains the REST API and WebSocket endpoint configurations.
- FinancialStreamer.Core: Defines the interfaces and models used across the application.
- FinancialStreamer.Infrastructure: Contains the services and configuration for interacting with the Tiingo API.
- FinancialStreamer.WebSocket: Manages WebSocket connections and message handling.
- wwwroot: Contains static files including
test_page.html
for testing the WebSocket and REST API endpoints.
-
Clone the Repository
git clone https://github.com/jalle007/FinancialStreamer.git cd FinancialStreamer
-
Build the Solution
dotnet build
-
Run the Solution
dotnet run --project FinancialStreamer.Api --launch-profile https
-
Start Python HTTP Server for Testing
cd FinancialStreamer.Api/wwwroot python -m http.server 8000
-
Access the Test Page and Swagger UI
- Test Page: http://localhost:8000/test_page.html
- Swagger UI: https://localhost:7057/swagger/index.html
-
Get Instruments
- URL:
/api/Prices/instruments
- Method:
GET
- Response: List of available financial instruments (e.g., EURUSD, USDJPY, BTCUSD)
- URL:
-
Get Price
- URL:
/api/Prices/{symbol}
- Method:
GET
- Response: Current price of the specified financial instrument
- URL:
-
Subscribe to Price Updates
- Connect to the WebSocket server at:
wss://localhost:7057/ws
- Send a message to subscribe:
{ "method": "SUBSCRIBE", "params": ["usdbyn", "gbpsgd"] }
- Connect to the WebSocket server at:
-
Unsubscribe from Price Updates
- Send a message to unsubscribe:
{ "method": "UNSUBSCRIBE", "params": ["usdbyn"] }
- Send a message to unsubscribe:
- Global Error Handling: Implemented via
ErrorHandlingMiddleware
, which catches and logs all unhandled exceptions, returning a standardized error response. - WebSocket Error Handling: Logs and handles exceptions during WebSocket message handling, ensuring the connection is cleanly closed on errors.
- Uses
ConcurrentDictionary
for efficient management of WebSocket subscribers. - Ensures only one active subscription per symbol to the data provider, reducing redundant connections.
- Detailed logging provides insights into performance and potential issues.