Events
Use an event to announce a fact other features can react to. In the App from Save and List Photos, publish photos.saved after the repository writes the record.
When to Use Events
Use inproc for subscribers in the same App process. Choose a transport-backed bus when subscribers run in other processes or hosts. Use a job on a queue for work with its own worker capacity or retry policy.
Generate an Event
Add events to the existing render.components list in .goforj.yml. Stop the development loop, then run these commands from the Project root.
forj render &&
forj make:event photos:saved --no-openThe generator writes internal/photos/saved_event.go with the photos.saved topic. Publish the event as a value created by the service. Event Subscribers adds subscriber registration separately. See make:event for naming, paths, and removal options.
Add the Payload
Replace the placeholder in SavedEvent with the saved record's ID. Keep the generated Topic method and topic constant.
// SavedEvent identifies a photo record that has been saved.
type SavedEvent struct {
PhotoID int64 `json:"photo_id"`
}Use stable topics so subscribers and transport configuration can depend on them.
Publishing
Add "log" and "your/module/internal/events" to the imports in internal/photos/service.go. Use the module path in go.mod. Add the bus field and constructor argument so Wire supplies the default event bus. Keep any other dependencies you added to Service.
// Service saves and lists photo records and announces each save.
type Service struct {
repo *PhotoRepo
bus events.Bus
}
// NewService receives the photo repository and configured event manager.
func NewService(repo *PhotoRepo, manager *events.Manager) *Service {
return &Service{repo: repo, bus: manager.Default()}
}In Save, replace the final return after Create succeeds with this code.
saved := Summary{ID: photo.Id.ValueOrZero(), StorageKey: photo.StorageKey}
if err := s.bus.WithContext(ctx).Publish(SavedEvent{PhotoID: saved.ID}); err != nil {
log.Printf("publish %s failed for photo id=%d", SavedEventTopic, saved.ID)
}
return saved, nilThis notification is best effort. The response reports the saved record, and publication failures go to the App log. When the database change and delivery must succeed as one durable workflow, use an outbox.
Drivers
EVENTS_SUPPORTED_DRIVERS determines the compiled backends. EVENTS_DRIVER selects the default bus driver at startup. EVENTS_AUDIT_DRIVER can select a driver for a named audit bus. The local inproc bus uses process memory. See the Environment Reference for available settings.
Verification
Add the photo subscriber, start forj dev, then save a photo. Expect the 201 response and a photos.saved log entry from the subscriber with the same ID.
When you use a distributed bus, send a test event through the publisher process and check for that ID in the subscriber process. Include duplicate delivery in that test. Readiness reports backend reachability, and the application test proves the subscription receives the right topic and payload.
Regeneration
Run forj build after changing supported drivers or named event buses. The build configured for an App in dev.apps runs automatically under forj dev. See Generation Commands for focused generators.
Next Steps
- Event Subscribers covers registration and reactions.
- Event Tests covers handler and transport checks.
- Events covers the standalone package and driver behavior.