#redis #pub-sub #connection-pool #message #publish #subscribe #kiss

redis-kiss

A really stupid approach to managing Redis PubSub

5 releases

0.1.4 Apr 21, 2023
0.1.3 Feb 21, 2022
0.1.2 Feb 21, 2022
0.1.1 Feb 18, 2022
0.1.0 Feb 5, 2022

#1317 in Database interfaces

Download history 88/week @ 2024-06-17 86/week @ 2024-06-24 49/week @ 2024-07-01 51/week @ 2024-07-08 52/week @ 2024-07-15 47/week @ 2024-07-22 34/week @ 2024-07-29 56/week @ 2024-08-05 28/week @ 2024-08-12 30/week @ 2024-08-19 80/week @ 2024-08-26 57/week @ 2024-09-02 63/week @ 2024-09-09 25/week @ 2024-09-16 70/week @ 2024-09-23 107/week @ 2024-09-30

270 downloads per month
Used in 4 crates

MIT license

13KB
203 lines

redis_kiss

Note: this now also exposes access to the Redis connection pool.

This is a pretty simple library intended for just consuming and producing messages for Redis PubSub, it does not follow "good programming practice" in the slightest but it does work and does simplify the work required.

The library manages a global pool of Redis connections and creates listener connections on the fly (due to some technical limitations).

Example

Publish something to Redis:

// Handle the Result<_, _>
redis_kiss::publish("channel", "data").await?;

// Log the error instead
redis_kiss::p("channel", "data").await;

Subscribe to Redis (as usual, see Redis documentation for more info):

// Create a new PubSub connection
let mut conn = redis_kiss::open_pubsub_connection().await?;

// Subscribe to something
conn.subscribe("test").await?;

// Handle incoming messages
let mut stream = conn.on_message();
while let Some(item) = stream.next().await {
    // Use Redis crate API as usual
    let channel = item.get_channel_name().to_string();

    // Decode the message using previously specified options
    // into a certain DeserializeOwned data type T, in this case String
    let payload: String = redis_kiss::decode_payload(&item).await?;

    // You can also choose a specific type on the fly
    use redis_kiss::PayloadType;
    let payload: String = redis_kiss::decode_payload(&item, &PayloadType::Msgpack).await?;

    // Do something with channel and payload
}

Environment Variables

The library is provided with options through the environment.

Name Description Default
REDIS_URI URI of the Redis server redis://localhost
REDIS_POOL_MAX_OPEN Maximum number of connections in Redis pool (for publishing data) 1000
REDIS_PAYLOAD_TYPE Data type to use when publishing data to Redis, either json, msgpack or bincode json
REDIS_KISS_LENIENT Whether to try to automatically decode other payload types if we fail to deserialize it using the chosen one 1

Dependencies

~9–21MB
~314K SLoC