Skip to content

EventBus is a library for implementing event-driven architecture in GoLang.

License

Notifications You must be signed in to change notification settings

optimus-hft/event-bus

Repository files navigation

Event Bus

pipeline codecov Go Report Card Go Reference

GoLang Library for implementing event-driven architecture

EventBus can be used to implement event-driven architectures in Golang, Each bus can have multiple subscribers to different topics.

Features

  • Listening on events using channels or callbacks with the ability to cancel subscriptions at any time.
  • Once subscriptions, After receiving the first event, Subscription is automatically cancelled.
  • Non-blocking publishing, Yet event ordering is guaranteed for each subscriber.

Getting Started

Installation

go get github.com/optimus-hft/event-bus

Usage

package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"
	"time"

	eventbus "github.com/optimus-hft/event-bus"
)

func main() {
	bus := eventbus.New[int]()
	channel, unsub := bus.Subscribe("t1")

	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
	defer stop()

	go func() {
		for {
			select {
			case event := <-channel:
				fmt.Println("event", event)
			case <-ctx.Done():
				unsub()
				return
			}
		}
	}()

	bus.Publish("t1", 1)
	bus.Publish("t1", 2)
	bus.Publish("t1", 3)

	time.Sleep(time.Second)
}

Contributing

Pull requests and bug reports are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

This project is licensed under the MIT License.

About

EventBus is a library for implementing event-driven architecture in GoLang.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages