#tungstenite #events #connection #event-listener #stream #error

stream-tungstenite

A streaming implementation of the Tungstenite WebSocket protocol

5 unstable releases

new 0.3.2 Oct 28, 2024
0.3.1 Oct 28, 2024
0.3.0 Oct 24, 2024
0.2.0 Oct 24, 2024
0.1.0 Oct 23, 2024

#65 in WebSocket

Download history 509/week @ 2024-10-23

509 downloads per month

Custom license

24KB
566 lines

stream-tungstenite

Overview

stream-tungstenite is a Rust library designed to facilitate the management of WebSocket connections with automatic reconnection capabilities. It provides a robust framework for handling WebSocket communication, including error handling, event listening, and customizable reconnection strategies.

Problem Statement

WebSocket connections can be unstable due to network issues, server downtime, or other unforeseen circumstances. This library addresses the following challenges:

  • Automatic Reconnection: Automatically attempts to reconnect when the connection is lost.
  • Error Handling: Provides structured error handling for various connection issues.
  • Event Management: Allows for easy management of events through listeners.

Features

  • Reconnect Options: Customize the reconnection strategy with options such as retry intervals and timeouts.
  • Event Listeners: Register multiple listeners to handle incoming messages and events.
  • Handshake Support: Implement custom handshake logic if needed.

Example Usage

Here’s a simple example of how to use stream-tungstenite to establish a WebSocket connection and handle messages:

use stream_tungstenite::{ReconnectT, ReconnectOptions};
use tokio_tungstenite::tungstenite::Message;
#[tokio::main]
async fn main() {
    let options = ReconnectOptions::default();
    let reconnect = ReconnectT::new("wss://example.com/socket", Some(options));
    // Start the connection
    reconnect.run().await;
}

In this example, a new ReconnectT instance is created with a specified WebSocket URL and default options. The run method initiates the connection and handles reconnections automatically.

Installation

To include stream-tungstenite in your project, add the following to your Cargo.toml:

[dependencies]
stream-tungstenite = "0.3.2"

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any enhancements or bug fixes.

License

This project is licensed under the Apache License Version 2.0. See the LICENSE file for more details.

Dependencies

~10–22MB
~318K SLoC