Basic LocalBus

The simplest way to use Go Event Bus for in-process event-driven communication.

Source: examples/01_basic_local_bus.go

Define Events

type UserCreated struct {
    UserID   string
    Email    string
    UserName string
}

func (e UserCreated) Name() string { return "user.created" }

type OrderPlaced struct {
    OrderID string
    UserID  string
    Total   float64
}

func (e OrderPlaced) Name() string { return "order.placed" }

Define Handlers

func SendWelcomeEmail(ctx context.Context, evt event.Event) error {
    e := evt.(UserCreated)
    slog.Info("sending welcome email", "user_id", e.UserID, "email", e.Email)
    return nil
}

func CreateUserProfile(ctx context.Context, evt event.Event) error {
    e := evt.(UserCreated)
    slog.Info("creating user profile", "user_id", e.UserID, "name", e.UserName)
    return nil
}

Wire It Up

chain := invoker.NewChain(
    invoker.NewMetrics(nil),
)

bus := event.NewLocalBus(event.LocalBusOptions{
    Invoker:       chain,
    MaxConcurrent: 10,
    OnErr: func(ctx context.Context, evt event.Event, err error, handler string) {
        slog.Error("handler failed", "event", evt.Name(), "handler", handler, "error", err)
    },
})

ctx := context.Background()

// Multiple handlers for the same event
bus.Subscribe(ctx, "user.created", SendWelcomeEmail, event.WithHandlerName("send-welcome-email"))
bus.Subscribe(ctx, "user.created", CreateUserProfile, event.WithHandlerName("create-user-profile"))
bus.Subscribe(ctx, "order.placed", NotifyWarehouse, event.WithHandlerName("notify-warehouse"))

// Synchronous dispatch
bus.EmitSync(ctx, UserCreated{
    UserID:   "user-123",
    Email:    "john@example.com",
    UserName: "John Doe",
})

// Asynchronous dispatch
bus.Emit(ctx, OrderPlaced{
    OrderID: "order-456",
    UserID:  "user-123",
    Total:   99.99,
})

Back to top

Copyright © 2025 Isaque de Souza Barbosa. Distributed under the MIT License.