A production-style low-latency market data feed built in C++17, with a real-time terminal dashboard. Mimics how exchanges like NSE/CME broadcast market data to subscribers.
┌─────────────┐ UDP Multicast 239.0.0.1:5000 ┌──────────────┐
│ Publisher │ ─────────────────────────────────▶│ Subscriber │
│ │ │ │
│ Retransmit │ ◀── RetransmitRequest (unicast) ── │ Gap Detect │
│ Store │ ──── Missing packets (unicast) ───▶│ + Buffer │
└──────┬──────┘ └──────┬───────┘
│ StatsMessage (UDP :5002) │ StatsMessage (UDP :5003)
▼ ▼
┌──────────────────────────────────────────────────────────────────┐
│ Stats Collector │
│ writes dashboard/data/stats.json │
└──────────────────────────┬───────────────────────────────────────┘
│ HTTP poll every 1s
▼
┌────────────────────────┐
│ Dashboard (HTML) │
│ Live charts + KPIs │
└────────────────────────┘
Requirements: CMake + Visual Studio 2019/2022 (or MinGW-w64)
build_windows.batThen run in three separate Command Prompts:
build\Release\publisher.exe
build\Release\subscriber.exe
build\Release\stats_collector.exeOpen dashboard/index.html in your browser.
Requirements: cmake, g++, pthread
chmod +x build_linux.sh
./build_linux.shThen run in three separate terminals:
./build/publisher
./build/subscriber
./build/stats_collectorOpen dashboard/index.html in your browser.
Note: On Linux you may need to add a multicast route:
sudo ip route add 239.0.0.1/32 dev lo
- Creates a UDP socket and sends
MarketMessagestructs to multicast group239.0.0.1:5000 - Each message has a monotonically increasing
sequence_number - Stores sent messages in a lock-free ring buffer (
RetransmitStore) for replay - Runs a retransmit handler thread on port 5001 — receives
RetransmitRequestfrom subscribers and resends missing packets - Broadcasts
StatsMessageto port 5002 every 500ms
- Joins the multicast group using
IP_ADD_MEMBERSHIP - Tracks
expected_seq— detects gaps whenmsg.sequence_number > expected_seq - On gap detection: sends a
RetransmitRequest(unicast) to the publisher - Buffers out-of-order messages in a
std::mapand drains them once the gap is filled - Measures per-message latency (
timestamp_nsin message - recv time) - Computes mean / p99 / max latency over a rolling window of 10,000 messages
- Sends
StatsMessageto port 5003 every 500ms
- Non-blocking UDP sockets on ports 5002 and 5003
- Merges publisher + subscriber stats into a single JSON snapshot
- Writes
dashboard/data/stats.jsonevery second - Keeps 120 snapshots of history for chart rendering
- Zero-dependency, self-contained HTML/CSS/JS
- Polls
data/stats.jsonevery 1 second - Shows: throughput chart, latency history, gap chart, live order book, event log, ticker
- Falls back to demo/simulation mode if no backend is running
All structs use #pragma pack(1) — no padding bytes. Safe to cast raw recv() bytes.
MarketMessage (49 bytes)
├── sequence_number uint64 8 bytes
├── timestamp_ns uint64 8 bytes
├── symbol char[8] 8 bytes (space-padded)
├── bid double 8 bytes
├── ask double 8 bytes
├── last_price double 8 bytes
├── volume uint64 8 bytes
└── msg_type uint8 1 byte
RetransmitRequest (22 bytes)
├── from_sequence uint64 8 bytes
├── to_sequence uint64 8 bytes
├── subscriber_ip uint32 4 bytes
└── subscriber_port uint16 2 bytes
| Mode | Throughput | Mean Latency | p99 Latency |
|---|---|---|---|
| UDP Multicast | ~2.1M msg/sec | 8–40 µs | 25–80 µs |
| TCP Unicast | ~340K msg/sec | 55–120 µs | 150–300 µs |
(Run on Ubuntu 22.04, i7-12700H, loopback interface)
To control publish rate:
./build/publisher 100 # 100µs between batches = ~80k msg/sec
./build/publisher 10 # 10µs between batches = ~800k msg/sec
./build/publisher 1 # 1µs between batches = ~2M msg/sec-
Byte order: All multi-byte integers in the struct are sent in host byte order. For multi-machine deployment, wrap each field with
htonl/htonson send andntohl/ntohson receive, or use a proper serialization format. -
Single publisher: The retransmit design assumes one publisher. For redundant publishers (hot-standby), add a publisher ID field to
MarketMessage. -
Gap buffer: The subscriber gap buffer uses
std::mapwith a lock. A lock-free MPSC queue would be more appropriate in production. -
Clock sync: Latency measurement assumes publisher and subscriber share the same clock (loopback). For multi-machine testing, use PTP/IEEE 1588 hardware timestamps.
-
Security: No authentication or encryption. Real exchanges use HMAC-signed headers or TLS at the gateway layer.
ChronosFeed/
├── CMakeLists.txt Build configuration
├── build_windows.bat Windows one-click build
├── build_linux.sh Linux/macOS one-click build
├── README.md This file
├── include/
│ ├── config.h Network constants
│ ├── message.h Wire-format structs
│ ├── platform.h Cross-platform socket/time
│ └── retransmit_store.h Lock-free ring buffer
├── src/
│ ├── publisher.cpp Multicast sender + retransmit handler
│ ├── subscriber.cpp Multicast receiver + gap detection
│ ├── stats_collector.cpp Stats aggregator → JSON
│ └── retransmit_store.cpp (stub)
└── dashboard/
├── index.html Live trading terminal UI
└── data/
└── stats.json Written by stats_collector at runtime