Skip to content

DarkPimbaa/hft-websocket-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HFT WebSocket Client

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.

Features

  • 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::document directly
  • 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

Dependencies

  • Linux (kernel with epoll support)
  • OpenSSL (for wss:// connections)
  • simdjson (for JSON parsing)
# Ubuntu/Debian
sudo apt install libssl-dev libsimdjson-dev

Installation

Header-only - just copy HftWsClient.hpp to your project:

#include "HftWsClient.hpp"

Usage

Async Client (Recommended for Trading Bots)

#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;
}

Blocking Client

#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;
}

Sending Messages

// Thread-safe send via spinlock (nanoscond-level contention)
std::string msg = R"({"method": "SUBSCRIBE", "params": ["ethusdt@trade"], "id": 1})";
client.send(msg);

Configuration

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 snapshots

Building

g++ main.cpp -o bot -O3 -lssl -lcrypto -lsimdjson

Performance Notes

  • 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 - onMessage callback runs on the epoll thread; share data with std::atomic or lock-free structures

Requirements

  • Linux
  • C++17
  • OpenSSL
  • simdjson

License

MIT

About

Ultra-low-latency header-only WebSocket client for HFT with epoll, CPU pinning, and simdjson

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors