created_at2024-03-14 16:44:52.213302
updated_at2024-05-15 17:10:30.783295
descriptionFramework for building low latency websocket client based applications.
Tom Brzozowski (HaveFunTrading)



Build Status Latest Version Docs Badge License Badge


BoomNet is a high-performance framework targeting development of low-latency network applications, particularly focusing on TCP stream-oriented clients that utilise various protocols.


Simply declare dependency on boomnet in your Cargo.toml and select desired features.

boomnet = { version = "0.0.25", features = ["full"]}

Design Principles

The framework is structured into multiple layers, with each subsequent layer building upon its predecessor, enhancing functionality and abstraction.


The first layer defines stream as abstraction over TCP connection, adhering to the following characteristics.

  • Must implement Read and Write traits for I/O operations.
  • Operates in a non-blocking manner.
  • Integrates with TLS using rustls.
  • Supports recording and replay of network byte streams.
  • Allows binding to specific network interface.
  • Facilitates implementation of TCP oriented client protocols such as WebSocket, HTTP, and FIX.

Streams are designed to be fully generic, avoiding dynamic dispatch, and can be composed in flexible way.

let stream: RecordedStream<TlsStream<TcpStream>> = TcpStream::bind_and_connect(addr, self.net_iface, None)?

Different protocols can then be applied on top of a stream.

let ws: Websocket<RecordedStream<TlsStream<TcpStream>>> = stream.into_websocket(self.url);


Selector provides abstraction over OS specific mechanisms (like epoll) for efficiently monitoring socket readiness events. Though primarily utilised internally, selectors are crucial for the IOService functionality, currently offering both mio and direct (no-op) implementations.

let mut io_service = MioSelector::new()?.into_io_service(IdleStrategy::Sleep(Duration::from_millis(1)));


The last layer manages lifecycle of endpoints and provides auxiliary services (such as asynchronous DNS resolution and auto disconnect) through the IOService.

Endpoint serves as low level construct for application logic. IOService oversees the connection lifecycle within endpoints.


The aim is to support a variety of protocols, including WebSocket, HTTP, and FIX, with WebSocket client functionality currently available.


The websocket client protocol complies with the RFC 6455 specification, offering the following features.

  • Compatibility with any stream.
  • TCP batch-aware timestamps for frames read in the same batch.
  • Not blocking on partial frame(s).
  • Designed for zero-copy read and write.
  • Optional masking of outbound frames.
  • Standalone usage or in conjunction with Selector and IOService.

Example Usage

The repository contains comprehensive list of examples.

The following example illustrates how to use multiple websocket connections with IOService in order to consume messages from the Binance cryptocurrency exchange. First, we need to define and implement our Endpoint. The framework provides TlsWebsocketEndpoint trait that we can use.

struct TradeEndpoint {
    id: u32,
    url: &'static str,
    instrument: &'static str,

impl TradeEndpoint {
    pub fn new(id: u32, url: &'static str, instrument: &'static str) -> TradeEndpoint {
        Self { id, url, instrument, }

impl TlsWebsocketEndpoint for TradeEndpoint {
    type Stream = MioStream;

    fn url(&self) -> &str {

    // called by the IO service whenever a connection has to be established for this endpoint
    fn create_websocket(&mut self, addr: SocketAddr) -> io::Result<TlsWebsocket<Self::Stream>> {
        // create secure websocket
        let mut ws = TcpStream::bind_and_connect(addr, None, None)?

        // send subscription message
            Some(format!(r#"{{"method":"SUBSCRIBE","params":["{}@trade"],"id":1}}"#, self.instrument).as_bytes()),


    fn poll(&mut self, ws: &mut TlsWebsocket<Self::Stream>) -> io::Result<()> {
        // keep calling receive_next until no more frames in the current batch
        while let Some(WebsocketFrame::Text(ts, fin, data)) = ws.receive_next()? {
            // handle the message
            println!("[{}] {ts}: ({fin}) {}",, String::from_utf8_lossy(data));

After defining the endpoint, it is registered with the IOService and polled within an event loop. The service handles Endpoint connection management and reconnection in case of disconnection.

fn main() -> anyhow::Result<()> {
    let mut io_service = MioSelector::new()?.into_io_service(IdleStrategy::Sleep(Duration::from_millis(1)));

    let endpoint_btc = TradeEndpoint::new(0, "wss://", None, "btcusdt");
    let endpoint_eth = TradeEndpoint::new(1, "wss://", None, "ethusdt");
    let endpoint_xrp = TradeEndpoint::new(2, "wss://", None, "xrpusdt");


    loop {
        // will never block

It is often required to expose shared state to the Endpoint. This can be achieved with user defined Context.

struct FeedContext {
    static_data: StaticData,

// use the marker trait
impl Context for FeedContext {}

When implementing our TradeEndpoint we can use TlsWebsocketEndpointWithContext trait instead.

impl TlsWebsocketEndpointWithContext<FeedContext> for TradeEndpoint {
    type Stream = MioStream;

    fn url(&self) -> &str {

    fn create_websocket(&mut self, addr: SocketAddr, ctx: &mut FeedContext) -> io::Result<TlsWebsocket<Self::Stream>> {
        // we now have access to context

    fn poll(&mut self, ws: &mut TlsWebsocket<Self::Stream>, ctx: &mut FeedContext) -> io::Result<()> {
        // we now have access to context

We will also need to create IOService that is Context aware.

let mut context = FeedContext::new(static_data);
let mut io_service = MioSelector::new()?.into_io_service_with_context(IdleStrategy::Sleep(Duration::from_millis(1)), &mut context);

The Context must now be passed to the service poll method.

loop {
    io_service.poll(&mut context)?;


BoomNet feature set is modular, allowing for tailored functionality based on project needs. The full feature enables all available features, while individual components can be enabled as needed.


Adds dependency on mio crate and enables MioSelector and MioStream.


Adds dependency on rustls crate and enables TlsStream and more flexible TlsReadyStream.


Adds support for Websocket protocol.

Commit count: 109

cargo fmt