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

stream-tungstenite

A streaming implementation of the Tungstenite WebSocket protocol

8 unstable releases (3 breaking)

new 0.4.2 Dec 20, 2024
0.4.1 Dec 16, 2024
0.4.0 Nov 21, 2024
0.3.2 Oct 28, 2024
0.1.0 Oct 23, 2024

#87 in WebSocket

Download history 256/week @ 2024-10-18 319/week @ 2024-10-25 28/week @ 2024-11-01 95/week @ 2024-11-15 58/week @ 2024-11-22 11/week @ 2024-11-29 9/week @ 2024-12-06 128/week @ 2024-12-13

301 downloads per month

Custom license

26KB
622 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.4.0"

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

~5–15MB
~204K SLoC