[ANN] EventBus - little and lightweight eventbus for GoLang.

1,056 views
Skip to first unread message

Alex Saskevich

unread,
Dec 19, 2014, 12:07:08 PM12/19/14
to golan...@googlegroups.com

EventBus (https://github.com/asaskevich/EventBus)

Package EventBus is the little and lightweight eventbus for GoLang.

Installation

Make sure that Go is installed on your computer. Type the following command in your terminal:

go get github.com/asaskevich/EventBus

After it the package is ready to use.

Import package in your project

Add following line in your *.go file:

If you unhappy to use long EventBus, you can do something like this:

Example

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");
}

Implemented methods

  • New()
  • Subscribe()
  • SubscribeOnce()
  • Unsubscribe()
  • Publish()

New()

New returns new EventBus with empty handlers.

bus := EventBus.New();

Subscribe(channel string, fn interface{})

Subscribe to a channel.

func Handler() { ... }
...
bus.Subscribe("channel:handler", Handler)

SubscribeOnce(channel string, fn interface{})

Subscribe to a channel once. Handler will be removed after executing.

func HelloWorld() { ... }
...
bus.SubscribeOnce("channel:handler", HelloWorld)

Unsubscribe(channel string)

Remove callback defined for a channel.

bus.Unsubscribe("channel:handler");

Publish(channel string, args ...interface{})

Execute callback defined for a channel. Any addional argument will be tranfered to the callback.

func Handler(str string) { ... }
...
bus.Subscribe("channel:handler", Handler)
...
bus.Publish("channel:handler", "Hello, World!");

Support

If you do have a contribution for the package feel free to put up a Pull Request or open Issue.

Henrik Johansson

unread,
Dec 19, 2014, 12:29:05 PM12/19/14
to Alex Saskevich, golang-nuts
Is it synchronous? Normally I would not expect publish to dispatch it for later invoke but I guess it can be useful synchronously as well.
However I think you really should make it safe for concurrent usage unless the use case is really not what I envision it to be for an event bus.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Saskevich

unread,
Dec 19, 2014, 4:58:17 PM12/19/14
to golan...@googlegroups.com, bwa...@gmail.com
Do you mean usage of 'sync' package (like mutexes or anything same) or executing callback functions in different threads?

пятница, 19 декабря 2014 г., 20:29:05 UTC+3 пользователь Henrik Johansson написал:

Henrik Johansson

unread,
Dec 19, 2014, 7:16:06 PM12/19/14
to Alex Saskevich, golang-nuts

It depends on the intended usage. If it is the idea that several goroutines can add listeners and publish events the you have to protect the map.

By synchronous I mean that the caller of publish have to wait for all registered subscribers to complete. That can be a valid use case but maybe not always what you want.

Alex Saskevich

unread,
Dec 20, 2014, 7:12:28 AM12/20/14
to golan...@googlegroups.com, bwa...@gmail.com
Now it's works like on example below:

package main

import (
"EventBus"
"time"
"fmt"
)

func A() {
time.Sleep(1000 * time.Millisecond)
fmt.Printf("A done\n")
}

func B() {
time.Sleep(1500 * time.Millisecond)
fmt.Printf("B done\n")
}

func C() {
time.Sleep(500 * time.Millisecond)
fmt.Printf("C done\n")
}

func main() {
bus := EventBus.New();
bus.Subscribe("A", A);
bus.Subscribe("B", B);
bus.Subscribe("C", C);

go func() {
bus.Publish("B");
bus.Publish("B");
bus.Publish("B");
}()

go func() {
bus.Publish("C");
bus.Publish("C");
bus.Publish("C");
}()

bus.Publish("A");
bus.Publish("A");
bus.Publish("A");

time.Sleep(9000 * time.Millisecond)
}

with output:
A done
A done
A done
B done
B done
B done
C done
C done
C done

I rightly understood it? :)

суббота, 20 декабря 2014 г., 3:16:06 UTC+3 пользователь Henrik Johansson написал:
Reply all
Reply to author
Forward
0 new messages