danielhookx / eventbus

Lightweight eventbus with Cross-process and async compatibility for Go.
Apache License 2.0
10 stars 2 forks source link
cross-process easy-to-use eventbus extensible lightweight non-intrusive

EventBus

EventBus is a lightweight event bus written in Golang, which provides various enhanced features and functionalities.

Features

Installation

Use go get to install this package.

go get github.com/danielhookx/xcontainer

Getting Started

Subscribe Topic And Publish

package main

import (
    "github.com/danielhookx/eventbus"
)

func calculator(a int, b int) {
    fmt.Printf("%d\n", a + b)
}

func main() {
    bus := eventbus.New()
    bus.Subscribe("calculator", calculator);
    bus.Publish("calculator", 10, 20);
}

Sync Subscribe Topic

bus.SubscribeSync("calculator", calculator);

Unsubscribe

bus.Unsubscribe("calculator", calculator)

SubscribeWith

There is an advanced usage of this, using the SubscribeWith method to customize the subscription behavior.

It should be noted that when using SubscribeWith, you need to ensure the uniqueness of the key yourself, and the same key will only be subscribed once.

package main

import (
    "github.com/danielhookx/eventbus"
    "github.com/danielhookx/fission"
)

type mockDist struct {
    key string
}

func createMockDistHandlerFunc(key any) fission.Distribution {
    return &mockDist{
        key: key.(string),
    }
}

func (m *mockDist) Register(ctx context.Context) {
    return
}
func (m *mockDist) Key() any {
    return m.key
}
func (m *mockDist) Dist(data any) error {
    // add your code here
    fmt.Println(data)
    return nil
}
func (m *mockDist) Close() error {
    return nil
}

func main() {
    topic := "main.test"
    bus := eventbus.New()
    bus.SubscribeWith(topic, "key1", createMockDistHandlerFunc)
    bus.Publish(topic, "jack")
    bus.Unsubscribe(topic, "key1")
}

Cross Process Events

You only need to specify the use of netbus during initialization. Later, you can use the cross-process eventbus like the local eventbus.

publisher process

package main

import (
    "github.com/danielhookx/eventbus"
)

func main() {
    rawURL := "tcp://:7633"
    remoteURL := "tcp://localhost:7634"

    bus := eventbus.New(
        eventbus.WithProxys(
            eventbus.NewRPCProxyCreator(rawURL, remoteURL),
        ),
    )

    bus.Publish("test", "jack")
}

subscriber process

package main

import (
    "github.com/danielhookx/eventbus"
)

func main() {
    rawURL := "tcp://:7634"
    remoteURL := "tcp://localhost:7633"

    bus := eventbus.New(
        eventbus.WithProxys(
            eventbus.NewRPCProxyCreator(rawURL, remoteURL),
        ),
    )

    bus.Subscribe("test", func(name string) {
        fmt.Printf("hello %s\n", name)
    })
}

Contributing

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