$ show rerun-io/ewebsock
ewebsock is a Rust WebSocket client library designed to compile and run both natively and on the web via WASM. It provides a simple, uniform API (`connect`, `ws_connect`, `ws_receive`) that abstracts away platform differences: on native it uses `tungstenite` with either a dedicated thread (default) or an opt-in `tokio` backend, while on web it wraps the browser `WebSocket` API via `web-sys`/`wasm-bindgen`. The library handles binary and text messages, ping/pong, connection lifecycle events, and supports TLS (rustls) and custom headers/subprotocols. It is small, well-documented, and heavily feature-flagged to keep dependencies minimal, making it suitable for GUI apps (e.g., egui) that need real-time communication across desktop and browser targets. The codebase is clean and exhibits strong craftsmanship: the separation of backends is clean, error handling is thorough (including read timeouts and graceful shutdown), and the web implementation carefully manages Blob/ArrayBuffer conversion and memory-safe closure lifetimes. The repository also includes an example egui app and an echo server for testing. While it is primarily a library, it is also an application in the sense that it ships executable examples. Overall, it is a mature, well-maintained crate with a clear API and robust platform abstraction.
The LLM identified 3 files worth highlighting for reuse:
Implements the WebSocket client for WASM using web-sys. Demonstrates non-obvious handling of various message payloads (ArrayBuffer, Blob, text) with FileReader fallback, robust JavaScript closure management (forgetting callbacks to prevent leaks), and correct error propagation. This pattern of safely wrapping browser APIs with Rust callbacks and ownership is directly reusable for any WASM project needing event-driven networking.
Provides the default native backend using a dedicated thread and std::sync::mpsc channel for sending. Shows a clean pattern for integrating a blocking socket with a non-blocking event loop via read timeouts and polling the channel, while also handling graceful shutdown when the sender is dropped. This approach can be extracted as a template for wrapping any blocking I/O API in a thread-safe, non-blocking faΓ§ade.
Contains a small, focused utility that converts `Options` into `tungstenite::WebSocketConfig` and builds a `ClientRequestBuilder` with custom headers and subprotocols. It demonstrates a clean conversion pattern that keeps configuration logic separate from connection logic, and could be reused or adapted for similar protocol wrappers.