Giter Club home page Giter Club logo

event-bus's Introduction

event-bus

Overview

This is an event bus library for usage in Go programs. It is a powerful and straightforward event management tool for Go applications. It enables easy subscription to and publication of events on various topics identified by string-based topics. It also allows publishers to supply data. This library is ideal for applications requiring a simple yet effective event-driven architecture.

Features

  • Event Subscription: Subscribe to events using string-based topics.
  • Generic Event Data: Publish events with generic/typed data.
  • Sequential Event Processing: Processes published events in sequence within a single Go routine.
  • Subscription Management: Manage subscriptions with subscriber IDs, allowing for easy unregistration.
  • Dynamic Buffer Size: Configure the internal buffer size for event handling.

Installation

Install the library using the following Go command:

go get github.com/enterprizesoftware/event-bus

Getting Started

Initialization

package main

import (
	"github.com/enterprizesoftware/event-bus"
)

func main() {
    bus := eventbus.New[string]()
    defer bus.Stop()
}

Subscribing to Topics

sub, err := bus.Subscribe("my-topic", func(msg string) error {
    fmt.Println("Received message:", msg)
    return nil
})
if err != nil {
    // handle subscription error
}

Publishing Events

bus.Publish("my-topic", "Hello, EventBus!")

Unsubscribing from Topics

// Handle - locates and removes the subscription with specified handle ID.
bus.Unsubscribe(eventbus.Handle(sub.Handle))

// Topic - removes all subscriptions for a givent topic.
bus.Unsubscribe(eventbus.Topic("my-topic"))

// Subscriber - removes all subscriptions for a given subscriber.
bus.Unsubscribe(eventbus.Subscriber(sub))

// Topic & Subscriber - removes all subscriptions for a given topic and subscriber.
bus.Unsubscribe(eventbus.Topic("my-topic"), eventbus.Subscriber(sub))

Error Handling

If an error or a panic occurs during the execution of messageHandler, then the system will call an error handler if one is provided.

sub, err := bus.Subscribe("my-topic", messageHandler, eventbus.Error(myErrorHandler))

Customizing EventBus

bus := eventbus.New[string](eventbus.BufferSize(200))

Contributing

We welcome contributions to this project. Please submit pull requests with your proposed changes or improvements.

License

This library is under the MIT License. See the LICENSE file in the repository for more details.

event-bus's People

Contributors

jamelt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.