GithubHelp home page GithubHelp logo

zekrotja / eventbus Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 5 KB

A go package to send and receive pub-sub messages using channels.

License: MIT License

Go 100.00%
event-driven eventbus events go go118 gogeneric pubsub hacktoberfest

eventbus's Introduction

~ eventbus ~

A go package to send and receive pub-sub messages using channels.

     

go get -u github.com/zekrotja/eventbus

Intro

This package provides a very simple, generic pub-sub event bus to simplify sending event messages between services using channels.

Why using channels over callbacks?

Callback are - in my opinion - not a very clean and performant way to perform event driven development in Go because, in contrast to languages like JavaScript, Go is not an event driven language. This can lead to blocking publisher routines while waiting for the execution of callbacks on the side of subscribers. Channels, which are well designed to communicate between go routines in the first place, are therefore a way better tool to achieve easy, performant and intuitive communication of events between publishers and subscribers.

Basic Example

package main

import (
	"bufio"
	"fmt"
	"os"

	"github.com/zekrotja/eventbus"
)

func subscriber(name string, bus *eventbus.EventBus[string]) {
	c, _ := bus.Subscribe()
	for msg := range c {
		fmt.Printf("[ -> %s ]: %s\n", name, msg)
	}
}

func main() {
	s := bufio.NewScanner(os.Stdin)
	bus := eventbus.New[string]()

	go subscriber("service1", bus)
	go subscriber("service2", bus)

	bus.SubscribeFunc(func(s string) {
		if s == "exit" {
			os.Exit(0)
		}
	})

	fmt.Println("Publish messages by writing them to the console.\nPress CTRL+C or write 'exit' to exit.")
	for s.Scan() {
		bus.Publish(s.Text())
	}
}

Further examples can be found in the examples directory. If you want to take alook at a practical example, feel free to explore my project Yuri69 which heavily depends on EventBus.

eventbus's People

Stargazers

 avatar

Watchers

 avatar  avatar  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.