Skip to content
/ pub Public

A go package based on the publish/subscribe model to execute independently executing functions,tasks or subscribers by sending events to its subscribers

Notifications You must be signed in to change notification settings

Vandathron/pub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vandathron/pub

Package vandathron/pub is designed based on the publish/subscribe model to execute independently executing functions,tasks or subscribers by sending events to its subscribers. Main Features:

  • Event creation. Events can be created by calling the CreateEvent method
  • Registering/Unregistering subscribers to events
  • Configurable logging. Interface Logger can be implemented to configure logging
  • Simple Concurrent execution of subscribers

Install

With go installed, you can install this package with this command

go get -u github.com/vandathron/pub

Examples

Let's start by creating a publisher with a single event

func main() {
	p := pub.NewPublisher()
	p.CreateEvent("USER.CREATED")
}

We can then declare subscribers, listeners or functions. Parameter must conform with the example below

func SendWelcomeEmail(data pub.EventPayload){
    u := data.Data.(User)
    fmt.Println(u.name)
    fmt.Println("Sending welcome mail..")
    ...
}

func SendSMS(data pub.EventPayload){
    u := data.Data.(User)
    fmt.Println(u.name)
    fmt.Println("Sending phone message..")
    ...
}

Subscribe listeners to event:

p.Subscribe("USER.CREATED", SendSMS, SendWelcomeEmail)

6 Event can be emitted by calling Publish(event string) method. This publishes event and execute subscribers concurrently.

p.Publish("USER.CREATED", pub.EventPayload{
    Data:   User{name: "van"},
    Header: pub.Header{},
})

Listeners can be unsubscribed from an event:

p.Unsubscribe(SendSMS, "USER.CREATED")

Logging is disabled by default. We can create a logger that implements the Logger interface.

type EvtLogger struct {

}

func (e *EvtLogger) LogInfo(msg string) {
    ...
}

func (e *EvtLogger) LogErr(msg string) {
    ...
}

Logging can now be enabled

p.DisableLogs = false
p.SetLogger(&EvtLogger)

Full Example

type EvtLogger struct {

}

var _ pub.Logger = new(EvtLogger)

func (e EvtLogger) LogInfo(msg string) {
   
}

func (e EvtLogger) LogErr(msg string) {
    
}

type User struct {
    name string
}

func main(){
	p := pub.NewPublisher()
	p.DisableLogs = false;
	p.SetLogger(&EvtLogger{})
	
	p.CreateEvent("USER.CREATED")
	p.CreateEvent("USER.DELETED")

	p.Subscribe("USER.CREATED", SendSMS)
    p.Subscribe("USER.CREATED", SendWelcomeEmail)

	p.Publish("USER.CREATED", pub.EventPayload{
		Data:   User{name: "van"},
		Header: pub.Header{},
	})

}

About

A go package based on the publish/subscribe model to execute independently executing functions,tasks or subscribers by sending events to its subscribers

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages