Simple synchronous event pub-sub package for Golang

event-go

CI Status Go Report Card MIT License release pkg.go.dev

Simple synchronous event pub-sub package for Golang

This is a Go language package for publishing/subscribing domain events. This is useful to decouple subdomains within applications. Here is a sketch for using this package in real world applications.

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/itchyny/event-go"
)

// Domain entity.
type User struct {
	ID      int64
	Created time.Time
	UserValue
}

// Domain value object.
type UserValue struct {
	Name  string
	Email string
}

// Domain event types.
const (
	EventTypeUserCreated event.Type = iota + 1
	EventTypeUserRetired
)

// Domain events.
type UserCreated struct{ User *User }
type UserRetired struct{ User *User }

// Define the type of the events.
func (ev *UserCreated) Type() event.Type { return EventTypeUserCreated }
func (ev *UserRetired) Type() event.Type { return EventTypeUserRetired }

type App struct{ event.Mapping }

func NewApp() *App {
	app := &App{event.NewMapping()}

	// Register mapping of event types and subscribers.
	app.
		On(
			EventTypeUserCreated,
			event.Func(func(ctx context.Context, ev event.Event) error {
				return app.SendNotification(ctx, NotifyUserCreated, ev.(*UserCreated).User)
			}),
		).
		On(
			EventTypeUserRetired,
			event.Func(func(ctx context.Context, ev event.Event) error {
				return app.SendNotification(ctx, NotifyUserRetired, ev.(*UserRetired).User)
			}),
		)

	return app
}

func (app *App) CreateUser(ctx context.Context, user *UserValue) (*User, error) {
	fmt.Printf("CreateUser: %#v\n", user)
	created := &User{1, time.Now(), *user}
	// Publish a domain event, instead of calling SendNotification directly.
	_ = app.Publish(ctx, &UserCreated{User: created})
	return created, nil
}

func (app *App) RetireUser(ctx context.Context, user *User) error {
	fmt.Printf("RetireUser: %#v\n", user)
	// Publish a domain event, instead of calling SendNotification directly.
	_ = app.Publish(ctx, &UserRetired{User: user})
	return nil
}

type NotificationType int

const (
	NotifyUserCreated NotificationType = iota + 1
	NotifyUserRetired
)

func (typ NotificationType) String() string {
	switch typ {
	case NotifyUserCreated:
		return "created"
	case NotifyUserRetired:
		return "retired"
	default:
		panic(typ)
	}
}

func (app *App) SendNotification(_ context.Context, typ NotificationType, user *User) error {
	fmt.Printf("SendNotification(%s): %#v\n", typ, user)
	// Send email or something.
	return nil
}

func main() {
	ctx := context.Background()
	app := NewApp()
	user, _ := app.CreateUser(ctx, &UserValue{"Test User", "[email protected]"})
	_ = app.RetireUser(ctx, user)
}

Bug Tracker

Report bug at Issues・itchyny/event-go - GitHub.

Author

itchyny (https://github.com/itchyny)

License

This software is released under the MIT License, see LICENSE.

Owner
itchyny
Professional of jq.
itchyny
Similar Resources

Simple, high-performance event streaming broker

Styx Styx is a simple and high-performance event streaming broker. It aims to provide teams of all sizes with a simple to operate, disk-persisted publ

Nov 24, 2022

Example Golang Event-Driven with kafka Microservices Choreography

Microservices Choreography A demonstration for event sourcing using Go and Kafka example Microservices Choreography. To run this project: Install Go I

Dec 2, 2021

:incoming_envelope: A fast Message/Event Hub using publish/subscribe pattern with support for topics like* rabbitMQ exchanges for Go applications

Hub 📨 A fast enough Event Hub for go applications using publish/subscribe with support patterns on topics like rabbitMQ exchanges. Table of Contents

Dec 17, 2022

Easy to use distributed event bus similar to Kafka

Easy to use distributed event bus similar to Kafka

chukcha Easy to use distributed event bus similar to Kafka. The event bus is designed to be used as a persistent intermediate storage buffer for any k

Dec 30, 2022

Discrete-event simulation in Go using goroutines

SimGo SimGo is a discrete event simulation framework for Go. It is similar to SimPy and aims to be easy to set up and use. Processes are defined as si

Sep 6, 2022

pubsub controller using kafka and base on sarama. Easy controll flow for actions streamming, event driven.

Psub helper for create system using kafka to streaming and events driven base. Install go get github.com/teng231/psub have 3 env variables for config

Sep 26, 2022

POC of an event-driven Go implementation

Event Driven example in Golang This POC shows an example of event-driven architecture with a working domain event broker, an event producer and a cons

Nov 2, 2021

Go library to build event driven applications easily using AMQP as a backend.

event Go library to build event driven applications easily using AMQP as a backend. Publisher package main import ( "github.com/creekorful/event" "

Dec 5, 2021

A basic event queue (and publisher/subscriber) in go

queue A basic event queue (and publisher/subscriber) in go. Installation go get github.com/jimjibone/queue Queue Usage Queue is a channel-based FIFO q

Dec 17, 2021
Related tags
ntfy is a super simple pub-sub notification service. It allows you to send desktop notifications via scripts.

ntfy ntfy (pronounce: notify) is a super simple pub-sub notification service. It allows you to send desktop and (soon) phone notifications via scripts

Jan 9, 2023
Simple-messaging - Brokerless messaging. Pub/Sub. Producer/Consumer. Pure Go. No C.

Simple Messaging Simple messaging for pub/sub and producer/consumer. Pure Go! Usage Request-Response Producer: consumerAddr, err := net.ResolveTCPAddr

Jan 20, 2022
golang long polling library. Makes web pub-sub easy via HTTP long-poll server :smiley: :coffee: :computer:
golang long polling library.  Makes web pub-sub easy via HTTP long-poll server :smiley: :coffee: :computer:

golongpoll Golang long polling library. Makes web pub-sub easy via an HTTP long-poll server. New in v1.1 Deprecated CreateManager and CreateCustomMana

Jan 6, 2023
nanoQ — high-performance brokerless Pub/Sub for streaming real-time data

nanoQ — high-performance brokerless Pub/Sub for streaming real-time data nanoQ is a very minimalistic (opinionated/limited) Pub/Sub transport library.

Nov 9, 2022
A basic pub-sub project using NATS

NATS Simple Pub-Sub Mechanism This is a basic pub-sub project using NATS. There is one publisher who publishes a "Hello World" message to a subject ca

Dec 13, 2021
Kafka, Beanstalkd, Pulsar Pub/Sub framework

go-queue Kafka, Beanstalkd, Pulsar Pub/Sub framework.

Sep 17, 2022
CLI tool for generating random messages with rules & publishing to the cloud services (SQS,SNS,PUB/SUB and etc.)

Randomsg A CLI tool to generate random messages and publish to cloud services like (SQS,SNS,PUB/SUB and etc.). TODO Generation of nested objects is no

Sep 22, 2022
Build event-driven and event streaming applications with ease

Commander ?? Commander is Go library for writing event-driven applications. Enabling event sourcing, RPC over messages, SAGA's, bidirectional streamin

Dec 19, 2022
Govent is an event bus framework for DDD event source implement

Govent is an event bus framework for DDD event source implement. Govent can also solve the package circular dependency problem.

Jan 28, 2022
Event-planning-go - GRAPHQL Project for Event Planning

About The Project GRAPHQL Project for Event Planning Building the project with l

Mar 13, 2022