rust-pubsub-lib provides a shared abstraction for publishing to, snapshotting from, and subscribing to broker-backed topics from Rust applications. It hides backend-specific connection details behind a small set of async traits so application code can stay focused on message handling instead of transport setup.
The primary abstractions provided by this library are the Publisher, Snapshot, and Subscriber traits.
Publisherasynchronously sends a message to a configured topic.Snapshotreads the currently available messages for a topic in one operation.Subscriberyields a stream of messages as they arrive. Backend errors are handled internally; the stream yields only successfully decoded messages.Messagedescribes the message shape used across all backends.
The library also provides concrete message helpers:
ByteMessagefor raw byte payloads.StringMessagefor UTF-8-oriented payloads, using lossy decoding when byte input is not valid UTF-8.
Select one or more crate features depending on the broker backend your application uses.
kafka: enableskafka_implwith Kafka-backed implementations ofPublisher,Snapshot, andSubscriber.redis-pubsub: enablesredis_impls::pubsubwith Redis pub/sub implementations ofPublisherandSubscriber.redis-stream: enablesredis_impls::streamwith Redis Stream implementations ofPublisher,Snapshot, andSubscriber.testing-utils: enables backend-specific test harness helpers such askafka_impl::testing_utilsfor tests that exercise broker-facing code.
All three streaming backends (Kafka, Redis pub/sub, and Redis Stream) share a single process-wide
runtime cache in src/cache.rs. Each (host, topic) pair reuses one background
task rather than spinning up a new connection per subscriber. Idle runtimes are evicted
automatically after a grace period once they have no active listeners.
The following environment variables are read by the Kafka backend:
KAFKA_CONNECTION_SECONDS: controls the timeout used by Kafka operations inget_kafka_timeout_val(). Defaults to1second if not set.
Redis-backed implementations do not read any crate-specific environment variables, but they do
require a valid Redis connection URI to be passed to Publisher::new(),
Snapshot::get(), or Subscriber::new().
Generate the crate documentation locally with cargo doc --all-features and open the output from target/doc/. The most important API entry points are documented in src/lib.rs, with backend-specific details under src/kafka_impl/mod.rs and src/redis_impls/mod.rs.
The following packages must be present on the host machine when building this library:
cmake(Or equivalent CMake provider)gcc/g++(Or equivalent C/C++ compiler/linker)libcurl4-openssl-dev(Or equivalentcurldev files)
The configured Dev Container in .devcontainer/ has the necessary tools to build without additional installation.