wstomp
A STOMP-over-WebSocket client library for Rust, built on top of awc and async-stomp.
This crate provides a simple client to connect to a STOMP-enabled WebSocket server (like RabbitMQ over Web-STOMP, or ActiveMQ). It handles the WebSocket connection, STOMP frame encoding/decoding, and WebSocket heartbeat (ping/pong) for you.
Features
- Connects to STOMP servers over WebSocket using
awc. - Handles all STOMP protocol encoding and decoding via
async-stomp. - Manages WebSocket ping/pong heartbeats automatically in a background task.
- Provides a simple
tokio::mpscchannel-based API ([WStompClient]) for sending and receiving STOMP frames. - Connection helpers for various authentication methods:
- [
connect]: Anonymous connection. - [
connect_with_pass]: Login and passcode authentication. - [
connect_with_token]: Authentication using an authorization token header.
- [
- Optional
rustlsfeature for SSL connections, with helpers that force HTTP/1.1 for compatibility with servers like SockJS.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0" # Replace with the actual version
= "2.0"
For SSL support, enable the rustls feature:
[]
= { = "0.1.0", = ["rustls"] }
Usage
Here is a basic example of connecting, subscribing to a topic, and receiving messages.
use ;
async
Connection with an Auth Token
If you need to pass an Authorization header (or any custom header):
use connect_with_token;
async
Connection with SSL (rustls feature)
If you are connecting to a wss:// endpoint and need SSL, use the rustls feature and the connect_ssl_* helpers.
These helpers are specially configured to force HTTP/1.1, which can be necessary for compatibility with some WebSocket servers (like those using SockJS).
// Make sure to enable the "rustls" feature in Cargo.toml
use connect_ssl_with_pass;
async
Auto-reconnect
Use [WStompConfig::build_and_connect_with_reconnection_cb] method to automatically perform a full reconnect upon errors.
use ;
async
Error Handling
The connection functions ([connect], [connect_ssl], etc.) return a Result<WStompClient, WStompConnectError>.
Once connected, the WStompClient::rx channel produces [WStompEvent] items, it may be a message or [WStompError].
-
WStompConnectError: An error that occurs during the initial WebSocket and STOMPCONNECThandshake. -
WStompError: An error that occurs after a successful connection.WsReceive/WsSend: A WebSocket protocol error.StompDecoding/StompEncoding: A STOMP frame decoding/encoding error.IncompleteStompFrame: A warning indicating that data was received but was not enough to form a complete STOMP frame. The client has dropped this data. This is often safe to ignore or log as a warning.WebsocketClosed: WebSocket was closed, possibly a reason fromawclibrary is inside.PingFailed: Couldn't send ping through the WebSocket protocol.PingTimeout: There was no pong for last ping.
License
This crate is licensed under "MIT" or "Apache-2.0".