Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Message ¶
type Message struct { Payload map[string]any `json:"payload"` // the JSON message Channel string `json:"channel"` ID string `json:"id"` // random UUID used to correlate messages across clients // contains filtered or unexported fields }
Message is the decomposed message as received from NOTIFY that we will send to the client. It needs to include the channel, and we provide an ID for all messages to help with correlation.
type MessagePoster ¶
type MessagePoster interface {
Post(message []byte)
}
MessagePoster is an interface which is can be implemented by any object that wants to listen in on messages posted from PG. The expectation is that posting the message itself will be done robustly by the websocket, for example by maintainin and internal queue of messages and dropping connections which can't keep up.
type NotifyRouter ¶
type NotifyRouter struct {
// contains filtered or unexported fields
}
func NewNotifyRouter ¶
func NewNotifyRouter() *NotifyRouter
func (*NotifyRouter) Post ¶
func (r *NotifyRouter) Post(audience string, message []byte)
Post posts a message to all registered channels for a audience. It's OK if there are no channels registered for a given audience, it just means nobody is listening at the moment.
func (*NotifyRouter) Register ¶
func (r *NotifyRouter) Register(audiences []string, p MessagePoster)
Register registers a new MessagePoster for the given audience.
func (*NotifyRouter) Unregister ¶
func (r *NotifyRouter) Unregister(audiences []string, p MessagePoster)
Unregister removes a MessagePoster from the given audience.
type PGListener ¶
type PGListener struct {
// contains filtered or unexported fields
}
func StartPGListener ¶
StartPGListener starts (and returns) a listener on the PG database.
func (*PGListener) AddPoster ¶
func (l *PGListener) AddPoster(pgChannels []string, audiences []string, p *WSPoster)
AddPoster adds a poster (websocket) to the listener. If the channels are not already being listened to, they are added.
func (*PGListener) RemovePoster ¶
func (l *PGListener) RemovePoster(pgChannels []string, audiences []string, p *WSPoster)
RemovePoster removes a poster (websocket) from the listener.
type PGWS ¶
type PGWS struct { PGChannels []string // The PG channels to LISTEN on Listener *PGListener // The listener associated with this WS endpoint GetAudience func(r *http.Request) []string // returns the audiences for this connection (defaults to 'default') }
func NewPGWS ¶
func NewPGWS(listener *PGListener, pgChannels ...string) *PGWS
func (*PGWS) ServeHTTP ¶
func (pgws *PGWS) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP accepts incoming HTTP connections, upgrades them to WebSockets, and serves the websocket. It only returns when the WebSocket closes. Note that there will be many calls to ServeHTTP for the same instance of PGWS. A new instance of WSPoster is created for each HTTP connection we upgrade.