GOEventBus

package module
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2024 License: MIT Imports: 11 Imported by: 0

README

EventBus

Simple event source system

This project lets you publish and subscribe events easily.

To download:

go get github.com/Shibbaz/GOEventBus

Quick Start

Let's make a pub/sub application:

  1. Create a project
mkdir demo
cd demo
go mod init demo
  1. Add main.go
package main

import (
	"fmt"

	gbus "github.com/Shibbaz/GOEventBus"
)

// the message entity to be dispatched
type HouseWasSold struct{}

func main() {
	dispatcher := &gbus.Dispatcher{
		"main.HouseWasSold": func(m map[string]any) {
			fmt.Printf("dispatch: %v\n",m)
		},
	}
	eventstore := gbus.NewEventStore(dispatcher)
	eventstore.Publish(gbus.NewEvent(
				HouseWasSold{},
				map[string]any{
					"price": 1 * 100,
				},
			))
	eventstore.Run()
}
  1. Get the dependency
go get github.com/Shibbaz/GOEventBus@v0.1.6.2
  1. Run the project
go run ./

Output:

2024/04/14 16:40:04 Event id of 6da96821-b27a-4db4-8f5f-e7a1e189b813 was published from channel 'd7a3c677-f328-4f76-addc-d11d64cde566'
2024/04/14 16:40:04 Channel a2cb010f-af65-4030-9e1e-44cdbd9baa5a was opened
dispatch: map[price:100]
...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EventStoreDB *sql.DB

Functions

func SetEventStoreDB

func SetEventStoreDB(psqlInfo string)

Setting database connection

Types

type Dispatcher

type Dispatcher map[string]func(*map[string]any) (map[string]interface{}, error)

type Event

type Event struct {
	Id         string
	Projection any
	Args       map[string]any
}

func NewEvent

func NewEvent(projection any, args map[string]any) Event

Event constructor

type EventStore

type EventStore struct {
	Dispatcher *Dispatcher
	Done       chan bool

	Wg sync.WaitGroup
	// contains filtered or unexported fields
}

func NewEventStore

func NewEventStore(dispatcher *Dispatcher) *EventStore

Eventstore constructor

func (*EventStore) Broadcast

func (eventstore *EventStore) Broadcast() error

Broadcasts every published event

func (*EventStore) GetEvent

func (eventstore *EventStore) GetEvent() any

Get event from sync.Pool

func (*EventStore) Publish

func (eventstore *EventStore) Publish(event Event)

Subscribing event

func (*EventStore) Query

func (eventstore *EventStore) Query(projection string) map[string](map[string]any)

querying database for events of projections'

func (*EventStore) Run

func (eventstore *EventStore) Run()

func (*EventStore) Setup

func (eventstore *EventStore) Setup(dbname string)

Setting up database and creating table

type EventStoreListener

type EventStoreListener struct {
	OnDescription chan string
	OnBye         chan bool
}

type EventStoreNode

type EventStoreNode struct {
	Listner EventStoreListener
	DC      webrtc.DataChannel
	// contains filtered or unexported fields
}

func NewEventStoreNode

func NewEventStoreNode(dispatcher Dispatcher) *EventStoreNode

EventStoreNode constructor

func (*EventStoreNode) HandleDataChannel

func (eventstore *EventStoreNode) HandleDataChannel(id string, event *Event)

func (*EventStoreNode) Publish

func (eventstore *EventStoreNode) Publish(event string)

Sending bytes over datachannels to publish events and send it in database Serializing data before inserting it to database

func (*EventStoreNode) Subscribe

func (eventstore *EventStoreNode) Subscribe(event Event)

Usecase of datachannel, event is encoded into bytes and send to another peer (look into Publish func)

type Serializer

type Serializer struct {
	// contains filtered or unexported fields
}

func NewSerializer

func NewSerializer() *Serializer

Serializer constructor

func (*Serializer) Deserialize

func (serializer *Serializer) Deserialize(data []byte) map[string]any

Deserialize deserializes data []byte to map[string]any

func (*Serializer) Serialize

func (serializer *Serializer) Serialize(data map[string]any) []byte

Serialize serializes data into []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL