Events | Goffee
Description: In Goffee defining events is straightforward, first you need to add the event name as a constant to the file events\event-names.go

Skip to main content

# Events

In `Goffee` defining events is straightforward, first you need to add the event name as a constant to the file `events\event-names.go`

#file: events\event-names.go

    package events

    const PASSWORD_CHANGED = "password-changed"
    const PROCESS_COMPLETED = "process-completed"

Next, you need to create the `event job`

### Event Job​

the event job is simply a function that gets executed every time the event is triggered, here is an example of one

#file: events\jobs

    package eventjobs

    import (
        "fmt"

        "git.smarteching.com/goffee/core"
        "git.smarteching.com/goffee/goffee/models"
    )

    var SendPasswordChangedNotificationEmail core.EventJob = func(event *core.Event, c *core.Context) {
        go func() {
            // logic to send the email goes here
        }()
    }

Next, you have to register the `event job` using the `events manager` so it can be executed when the event is triggered, you can simply do that in the file `register-events.go`

    package main

    import (
        "git.smarteching.com/goffee/core"
        "git.smarteching.com/goffee/goffee/events"
        eventjobs "git.smarteching.com/goffee/goffee/events/jobs"
    )

    // Register events
    func registerEvents() {
        eventsManager := core.ResolveEventsManager()
        //########################################
        //#      events registration         #####
        //########################################

        // register your event here...
        eventsManager.Register(events.PASSWORD_CHANGED, eventjobs.SendPasswordChangedNotificationEmail)
    }

### Triggering an event​

Events can be triggered using the `events manager`, here is how

    package controllers

    import (
        "git.smarteching.com/goffee/core"
        "git.smarteching.com/goffee/goffee/events"
        "git.smarteching.com/goffee/goffee/models"
    )

    func Signup(c *core.Context) *core.Response {
        var user models.User
        err = c.GetEventsManager().Fire(
            &core.Event{
                Name: events.PASSWORD_CHANGED,
                Payload: map[string]interface{}{
                    "user": user,
                }
        })
    }