Event Publisher/Emitter

Event Publisher provides an idiomatic asynchronous event-driven architecture that able to Publish asynchronous and synchronous events. Subscribe and Unsubscribe to those events.

aah Server events built around this Event Publisher.

On each named event you can attach one or more callbacks to it. The callback have to be compliant with aah.EventCallback or callback func is aah.EventCallbackFunc.

Table of Contents

Event Structure

// Event type holds the details of single event.
Event struct {
	Name string
	Data interface{}
}

Subscribe to the Event

// compliant with aah.EventCallbackFunc
func productUpdated(e *aah.Event)  {
  // logic goes here
}

subscribe := &EventCallback{
  Callback: productUpdated,
  CallOnce: false, // always called when event is published
}

aah.SubscribeEvent("ProductUpdated", subscribe)

// OR

// subscribe using callback func
aah.SubscribeEventFunc("ProductUpdated", productUpdated)

Unsubscribe from the Event

// compliant with aah.EventCallbackFunc
func productUpdated(e *aah.Event)  {
  // logic goes here
}

subscribe := EventCallback{Callback: productUpdated}

// you have subscribe it before

aah.UnsubscribeEvent("ProductUpdated", subscribe)

// OR

// unsubscribe by callback func
aah.UnsubscribeEventFunc("ProductUpdated", productUpdated)

Publish Event Asynchronously

aah.PublishEvent("UserEdited", data)

Publish Event Synchronously

aah.PublishEventSync("UserEdited", data)