cafe-plaetschwiesle/internal/controller/events.go
2023-07-04 23:18:05 +02:00

32 lines
551 B
Go

package controller
import (
"encoding/json"
"github.com/r3labs/sse/v2"
)
const ServerSideEvent = "sse"
type NotifierType uint
const (
Create NotifierType = iota
Delete
DeleteAll
)
type StatusMessage struct {
Type NotifierType `json:"type"`
Payload []Order `json:"payload"`
}
func (c *Controller) setupEventChannel() {
c.SSE.AutoReplay = false
c.SSE.CreateStream(ServerSideEvent)
}
func (c *Controller) publishMessage(msg StatusMessage) {
json, _ := json.Marshal(msg)
c.SSE.Publish(ServerSideEvent, &sse.Event{Data: json})
}