#drop #sockets #websocket-client #delay #determine #future

reconnecting-websocket

A wrapper around gloo::net::websocket::futures::WebSocket that reconnects when the socket drops. Uses exponential-backoff::Backoff to determine the delay between reconnects

5 unstable releases

0.3.2 Jul 26, 2024
0.3.1 Jul 21, 2024
0.3.0 Jul 15, 2024
0.2.0 Jul 15, 2024
0.1.0 Jul 14, 2024

#69 in WebSocket

Download history 154/week @ 2024-07-22 16/week @ 2024-07-29 15/week @ 2024-09-16 3/week @ 2024-09-23 9/week @ 2024-09-30

230 downloads per month

MIT license

52KB
797 lines

Crate Documentation Workflow Status Maintenance

reconnecting-websocket

A wrapper around WebSocket that reconnects when the socket drops. Uses Backoff to determine the delay between reconnects

Features

  • tracing - enables the tracing crate and logs everything it's doing
  • state-events - changes the Item type of the stream to be an enum that is either a message or a status change Both are enabled by default

Usage

Input means stuff you want to send from this client to the server (Sink)

Outut means stuff you want to receive back from the server (Source)

  1. Implement TryFrom for Message for your input type
  2. Implement TryFrom<Message> for your output type
  3. Both input and output need to implement Unpin and, if using tracing feature, Debug
  4. Use SocketBuilder to set the URL and configure backoff. get_proto_and_host can help constructing the URL relative to the current window.location
  5. Call SocketBuilder::open to connect the socket. The errors open returns are likely fatal (invalid URL, blocked port), see WebSocket::open for details. The first connect is done in the builder so it fails fast if these fatal errors occur but the same kind of error can also occur on any reconnect and be returned by the Socket Stream implementation
  6. The returned Socket can then be polled to get incoming messages. Socket::send can be called to send messages or Socket::get_sender can be used to get an UnboundedSender. Socket::close or dropping it will drop the inner WebSocket which sends a close frame and cleans up the event handlers

Example

tests/reconnect.rs

Docs

Code docs

License

MIT

Dependencies

~15MB
~276K SLoC