Reacting to each tick events from the websocket

171 views Asked by At

I would like to design my trading system reacting to each tick events from the websocket stream i subscribed to.

So basically i have two options :

void WebsocketClient::on_write(beast::error_code ec,
                               std::size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);

    ws_.async_read(buffer_,
                   beast::bind_front_handler(&WebsocketClient::on_message,
                                             shared_from_this()));
}

void WebsocketClient::on_message(beast::error_code ec,
                                 std::size_t bytes_transferred) {

    // signal generation and sending http request to place new orders here
    // first before calling async_read() below

    std::cout << "Received: " << beast::buffers_to_string(buffer_.cdata())
              << std::endl;

    ws_.async_read(buffer_,
                   beast::bind_front_handler(&WebsocketClient::on_message,
                                             shared_from_this()));
}

Or i could

void WebsocketClient::on_message(beast::error_code ec,
                            std::size_t bytes_transferred) {
    ws_.async_read(buffer_,
                   beast::bind_front_handler(&WebsocketClient::on_message,
                                             shared_from_this()));

    // signal generation and sending http request to place new orders here after
    // calling async_read()

    std::cout << "Received: " << beast::buffers_to_string(buffer_.cdata())
              << std::endl;
}

please give me your suggestions and other ideas i could look at! advance thanks!

1

There are 1 answers

5
sehe On BEST ANSWER

There would be no tangible difference, unless

// signal generation and sending http request to place new orders here
// first before calling async_read() below

either

  • takes significant amount of time (design smell on IO threads)
  • exits the function by return/exception

That's because async_read by definition always returns immediately