A Pub/Sub library for Rust backed by Postgres

Overview

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> {
    // Connect to the database
    let pool = PgPool::connect("your_database_url").await?;
    let mut pubsub = PubSub::new(pool).await?;

    // Subscribe to the topic
    pubsub.subscribe("my_topic", |message| {
        async {
            // Print the received message
            println!("Received message: {:?}", message);

            // Simulate some asynchronous work with the received message
            tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
            println!("Finished processing message: {:?}", message);

            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.

You might also like...
An easy-to-use, zero-downtime schema migration tool for Postgres

Reshape is an easy-to-use, zero-downtime schema migration tool for Postgres. It automatically handles complex migrations that would normally require downtime or manual multi-step changes.

postgres-ical - a PostgreSQL extension that adds features related to parsing RFC-5545 « iCalendar » data from within a PostgreSQL database

postgres-ical - a PostgreSQL extension that adds features related to parsing RFC-5545 « iCalendar » data from within a PostgreSQL database

Postgres Foreign Data Wrapper for Clerk.com API

Pre-requisites Postgres-15 Rust pgrx Getting Started To run the program locally, clone the repository git clone https://github.com/tembo-io/clerk_fdw.

CouchDB client-side library for the Rust programming language

Chill Chill is a client-side CouchDB library for the Rust programming language, available on crates.io. It targets Rust Stable. Chill's three chief de

An etcd client library for Rust.

etcd An etcd client library for Rust. etcd on crates.io Documentation for the latest crates.io release Running the tests Install Docker and Docker Com

Redis library for rust

redis-rs Redis-rs is a high level redis library for Rust. It provides convenient access to all Redis functionality through a very flexible but low-lev

Pure Rust library for Apache ZooKeeper built on MIO

rust-zookeeper Zookeeper client written 100% in Rust This library is intended to be equivalent with the official (low-level) ZooKeeper client which sh

Mysql client library implemented in rust.

mysql This crate offers: MySql database driver in pure rust; connection pool. Features: macOS, Windows and Linux support; TLS support via nativetls cr

GlueSQL is a SQL database library written in Rust

GlueSQL is a SQL database library written in Rust. It provides a parser (sqlparser-rs), execution layer, and optional storage (sled) packaged into a single library.

Owner
Nick Rempel
Nick Rempel
rust-postgres support library for the r2d2 connection pool

r2d2-postgres Documentation rust-postgres support library for the r2d2 connection pool. Example use std::thread; use r2d2_postgres::{postgres::NoTls,

Steven Fackler 128 Dec 26, 2022
A tokio-uring backed runtime for Rust

tokio-uring A proof-of-concept runtime backed by io-uring while maintaining compatibility with the Tokio ecosystem. This is a proof of concept and not

Tokio 726 Jan 4, 2023
Appendable and iterable key/list storage, backed by S3, written in rust

klstore Appendable and iterable key/list storage, backed by S3. General Overview Per key, a single writer appends to underlying storage, enabling many

Eric Thill 3 Sep 29, 2022
Simple and handy btrfs snapshoting tool. Supports unattended snapshots, tracking, restoring, automatic cleanup and more. Backed with SQLite.

Description Simple and handy btrfs snapshoting tool. Supports unattended snapshots, tracking, restoring, automatic cleanup and more. Backed with SQLit

Eduard Tolosa 27 Nov 22, 2022
Manage Redshift/Postgres privileges in GitOps style written in Rust

grant-rs An open-source project that aims to manage Postgres/Redshift database roles and privileges in GitOps style, written in Rust. Home | Documenta

Duyet Le 13 Nov 23, 2022
cogo rust coroutine database driver (Mysql,Postgres,Sqlite)

cdbc Coroutine Database driver Connectivity.based on cogo High concurrency,based on coroutine No Future<'q,Output=*>,No async fn, No .await , no Poll*

co-rs 10 Nov 13, 2022
A Rust application that inserts Discogs data dumps into Postgres

Discogs-load A Rust application that inserts Discogs data dumps into Postgres. Discogs-load uses a simple state machine with the quick-xml Rust librar

Dylan 7 Dec 9, 2022
Command-line tool to make Rust source code entities from Postgres tables.

pg2rs Command-line tool to make Rust source code entities from Postgres tables. Generates: enums structs which can be then used like mod structs; use

Stanislav 10 May 20, 2022
Making Postgres and Elasticsearch work together like it's 2021

Making Postgres and Elasticsearch work together like it's 2021 Readme ZomboDB brings powerful text-search and analytics features to Postgres by using

ZomboDB 4.2k Jan 2, 2023
Distributed, version controlled, SQL database with cryptographically verifiable storage, queries and results. Think git for postgres.

SDB - SignatureDB Distributed, version controlled, SQL database with cryptographically verifiable storage, queries and results. Think git for postgres

Fremantle Industries 5 Apr 26, 2022