Skip to content

InvalidJoker/bell

Repository files navigation

Bell

A lightweight Kotlinx Serialization–powered event bus with pluggable drivers (Redis, in-memory, etc.), annotation-based listeners, and coroutine support.

Installation

repositories {
    mavenCentral()
}

dependencies {
    implementation("dev.invalidjoker:bell:<latest-version>")
}

1. Define an Event

All events extend Notification and use Kotlinx Serialization:

import kotlinx.serialization.Serializable

@Serializable
data class TestNotification(
    val foo: String,
    val bar: String,
) : Notification(
    channel = "test",
    type = "notification"
)

2. Listeners

2.1 Annotated Listener

class MyListener {

    @NotificationListener
    suspend fun onTestNotification(event: TestNotification) {
        println("Received via annotation:")
        println("  foo = ${event.foo}")
        println("  bar = ${event.bar}")
    }
}

2.2 Inline Listener (onEvent<T>())

engine.onEvent<TestNotification> { event ->
    println("Received inline:")
    println("  foo = ${event.foo}")
    println("  bar = ${event.bar}")
}

Inline listeners work for any event type and do not require annotations.


3. Drivers

Bell supports different backends through pluggable drivers.


3.1 Redis Driver (distributed)

import de.joker.bell.NotificationEngine
import de.joker.bell.drivers.RedisDriver
import redis.clients.jedis.JedisPool

fun main() {
    val jedis = JedisPool("localhost", 6379)

    val engine = NotificationEngine(
        RedisDriver(jedis, "notifications")
    )

    // Connect to Redis
    engine.connect()

    // Register annotated listener
    engine.registerAnnotated(MyListener())

    // Register inline listener
    engine.onEvent<TestNotification> { event ->
        println("Inline: $event")
    }

    // Send an event
    engine.send(TestNotification("Hello", "World"))
}

3.2 LocalDriver (in-memory)

This driver sends notifications only inside the same JVM, great for tests or local development.

Usage example:

import de.joker.bell.NotificationEngine
import de.joker.bell.drivers.LocalDriver

fun main() {
    val engine = NotificationEngine(LocalDriver())

    // Connect
    engine.connect()

    // Register inline listener
    engine.onEvent<TestNotification> { event ->
        println("Local received:")
        println("  foo = ${event.foo}")
        println("  bar = ${event.bar}")
    }

    // Register annotated listener (optional)
    engine.registerAnnotated(MyListener())

    // Send event
    engine.send(TestNotification("Local", "Driver"))
}

About

Kotlinx Serialization–powered event bus

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages