unisub 0.1.0

A Pub/Sub library for Rust backed by Postgres
docs.rs failed to build unisub-0.1.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: unisub-0.1.2

Unisub

Crates.io Docs.rs

Unisub is a Pub/Sub library for Rust, using Postgres as the backend. It offers a convenient way to publish and subscribe to messages across different topics.

Features

  • Publish messages to topics
  • Subscribe to topics to receive messages
  • Create and remove topics
  • Includes a convenience binary for managing topics and running migrations
  • Asynchronous design using Tokio

Installation

Add Unisub as a dependency in your Cargo.toml:

[dependencies]
unisub = "*"

Setting up the Postgres Environment

  1. Install Postgres if you haven't already. You can download it from here.
  2. Create a new database and note the connection URL.
  3. Make sure your Postgres database is accessible and running.

Environment Variable

Set an environment variable called DATABASE_URL with the Postgres connection URL.

export DATABASE_URL=postgres://username:password@localhost/dbname

Running Migrations and Setting Up Topics

Using CLI

First, run the migrations:

unisub migrate

Then you can add your topics:

unisub add-topic my_topic

Programmatically

use unisub::PubSub;
use unisub::migrate;
use sqlx::PgPool;

#[tokio::main]
async fn main() -> Result<(), unisub::Error> {
let pool = PgPool::connect("your_database_url").await?;
migrate(&pool).await?;
let mut pubsub = PubSub::new(pool).await?;
pubsub.add_topic("my_topic").await?;
Ok(())
}

Usage

Library

use unisub::PubSub;
use sqlx::PgPool;

#[tokio::main]
async fn main() -> Result<(), unisub::Error> {
let pool = PgPool::connect("your_database_url").await?;
let mut pubsub = PubSub::new(pool).await?;
pubsub.push("my_topic", b"My message").await?;
Ok(())
}

And here's an example of how to subscribe to a topic:

use unisub::PubSub;
use sqlx::PgPool;

#[tokio::main]
async fn main() -> Result<(), unisub::Error> {
let pool = PgPool::connect("your_database_url").await?;
let mut pubsub = PubSub::new(pool).await?;
pubsub.subscribe("my_topic", |message| {
println!("Received message: {:?}", message);
async { Ok(()) }
}).await?;
Ok(())
}

Contributing

Contributions are welcome! Please submit a pull request or create an issue to get started.

License

This project is licensed under the MIT License.