EventBus

repository·master·Indexed 24 days ago

https://github.com/asaskevich/eventbus

A lightweight, asynchronous event bus for GoLang designed for decoupled communication within an application or across different processes via RPC. It provides functionality for subscribing to topics (synchronously, asynchronously, or once), publishing events, and managing cross-process events using a client-server model.

Tokens
2.2K
Snippets
11
Records
23
Agent score
82%

What's inside asaskevich-eventbus

  1. Implement Cross-Process Events

    master

    EventBus supports cross-process communication using a client-server model via RPC services.

    • Server Service: Listens to client subscriptions and can publish events.
    • Client Service: Listens to remotely published events from a server.

    Server Implementation Example:

    func main() {
        server := NewServer(":2010", "/_server_bus_", New())
        server.Start()
        server.EventBus().Publish("main:calculator", 4, 6)
        server.Stop()
    }

    Client Implementation Example:

    func main() {
        client := NewClient(":2015", "/_client_bus_", New())
        client.Start()
        // Subscribe to events from the server at :2010 on the /_server_bus_ path
        client.Subscribe("main:calculator", calculator, ":2010", "/_server_bus_")
        client.Stop()
    }
  2. Import EventBus in your Go project

    master

    You can import the package using its full path. If you prefer a shorter alias, you can use evbus.

    // Standard import
    import "github.com/asaskevich/EventBus"
    
    // Using an alias
    import (
    	evbus "github.com/asaskevich/EventBus"
    )
  3. Configure subscription types with SubscribeType

    master

    When registering a subscription via SubscribeArg, use the SubscribeType to define the lifecycle of the subscription:

    • Subscribe (value 0): The client will receive all events published to the specified topic.
    • SubscribeOnce (value 1): The client will receive only the next single event published to the topic, after which the subscription is effectively finished for that event.
  4. Basic usage of EventBus

    master

    The basic workflow involves creating a new bus, subscribing a handler function to a topic, publishing data to that topic, and optionally unsubscribing.

    func calculator(a int, b int) {
    	fmt.Printf("%d\n", a + b)
    }
    
    func main() {
    	bus := EventBus.New();
    	bus.Subscribe("main:calculator", calculator);
    	bus.Publish("main:calculator", 20, 40);
    	bus.Unsubscribe("main:calculator", calculator);
    }
  5. Subscribe to a topic asynchronously with SubscribeAsync()

    master

    Registers an asynchronous callback.

    Parameters:

    • topic string: The topic name.
    • fn interface{}: The handler function.
    • transactional bool: If true, subsequent callbacks for this topic are run serially. If false, they are run concurrently.

    Returns an error if fn is not a function.

    func slowCalculator(a, b int) {
    	time.Sleep(3 * time.Second)
    	fmt.Printf("%d\n", a + b)
    }
    
    bus := EventBus.New()
    bus.SubscribeAsync("main:slow_calculator", slowCalculator, false)
    
    bus.Publish("main:slow_calculator", 20, 60)
    
    // Wait for all async callbacks to complete
    bus.WaitAsync()
  6. Publish events with Publish()

    master

    Executes all callbacks registered to the specified topic. Any additional arguments passed to Publish are transferred directly to the callback functions.

    func Handler(str string) { ... }
    
    // Usage
    bus.Subscribe("topic:handler", Handler)
    bus.Publish("topic:handler", "Hello, World!")
  7. Subscribe to a topic once with SubscribeOnce()

    master

    Registers a handler that will be automatically removed from the topic after its first execution. Returns an error if fn is not a function.

    func HelloWorld() { ... }
    
    // Usage
    bus.SubscribeOnce("topic:handler", HelloWorld)