A high-performance, header-only C++17 WebSocket client library designed for High Frequency Trading applications. Built on raw Linux epoll with zero-copy JSON parsing via simdjson.
- Ultra-low latency - Edge-triggered epoll with non-blocking sockets
- CPU pinning - Pin network thread to a specific core for deterministic latency
- Header-only - Just include
HftWsClient.hpp - simdjson integration - Callbacks receive parsed
simdjson::ondemand::documentdirectly - Spinlock writes - Thread-safe send with nanosecond-level lock contention
- Auto ping/pong - Protocol-level keepalive with configurable periodic pings
- Message fragmentation - RFC 6455 Section 5.4 compliant reassembly
- Async and sync modes - Background thread or blocking event loop
- Linux (kernel with epoll support)
- OpenSSL (for
wss://connections) - simdjson (for JSON parsing)
# Ubuntu/Debian
sudo apt install libssl-dev libsimdjson-devHeader-only - just copy HftWsClient.hpp to your project:
#include "HftWsClient.hpp"#include "HftWsClient.hpp"
#include <iostream>
int main() {
HftWs::Client client;
// Pin WebSocket thread to Core 0 for deterministic latency
client.pinThread(0);
// Connect in background - callback runs on the WebSocket thread
client.connectAsync("wss://stream.binance.com:9443/ws/btcusdt@trade",
[](simdjson::ondemand::document& doc) {
// Zero-copy extraction (std::string_view, no allocation)
std::string_view price = doc["p"];
std::string_view qty = doc["q"];
std::cout << "Price: " << price << " | Qty: " << qty << "\n";
}
);
// Main thread is free for trading logic
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
client.close();
return 0;
}#include "HftWsClient.hpp"
#include <iostream>
int main() {
HftWs::Client client;
// Blocks and enters event loop immediately
client.connect("wss://stream.binance.com:9443/ws/btcusdt@aggTrade",
[](simdjson::ondemand::document& doc) {
std::cout << "Trade: " << std::string_view(doc["p"]) << "\n";
}
);
return 0;
}// Thread-safe send via spinlock (nanoscond-level contention)
std::string msg = R"({"method": "SUBSCRIBE", "params": ["ethusdt@trade"], "id": 1})";
client.send(msg);client.setPingTime(3000); // Ping every 3 seconds
client.setAutoPing(true); // Enable periodic pings
client.setAutoPong(false); // Disable auto pong (default: on)
client.setMaxMessageSize(8 * 1024 * 1024); // 8MB for large snapshotsg++ main.cpp -o bot -O3 -lssl -lcrypto -lsimdjson- Zero-copy parsing - simdjson parses directly from aligned internal buffers
- Frame reassembly - Fragmented messages accumulated in pre-allocated buffer, avoiding reallocations in hot path
- Lock-free writes - Spinlock avoids context switches and sleep states
- Thread safety -
onMessagecallback runs on the epoll thread; share data withstd::atomicor lock-free structures
- Linux
- C++17
- OpenSSL
- simdjson
MIT