Documentation ¶
Overview ¶
Package presence provides an advanced presence system
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Prefix for presence package Prefix = "presence" // ErrInvalidID for stating the event id is not valid ErrInvalidID = errors.New("invalid id") // ErrInvalidStatus for stating the event status is not valid ErrInvalidStatus = errors.New("invalid status") )
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend interface { Online(...string) error Offline(...string) error Status(...string) ([]Event, error) Close() error Error() chan error ListenStatusChanges() chan Event }
Backend represents basic interface for all required backend operations for presence package
type Error ¶
Error holds the non-thread safe errors for given specific ids
type Event ¶
type Event struct { // ID is the given key by the application ID string // Status holds the changing type of event Status Status }
Event is the data type for occuring events in the system
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis holds the required connection data for redis
Example (ListenStatusChanges) ¶
connStr := os.Getenv("REDIS_URI") if connStr == "" { connStr = "localhost:6379" } backend, err := NewRedis(connStr, 10, time.Second*1) if err != nil { fmt.Println(err.Error()) } // adjust config for redis instance c := backend.(*Redis).redis.Pool().Get() if _, err := c.Do("CONFIG", "SET", "notify-keyspace-events", "Ex$"); err != nil { fmt.Println(err) } if err := c.Close(); err != nil { fmt.Println(err) } s, err := New(backend) if err != nil { fmt.Println(err.Error()) } go func() { for event := range s.ListenStatusChanges() { switch event.Status { case Online: fmt.Println(event) case Offline: fmt.Println(event) } } }() go func() { s.Online("id") }() // wait for events <-time.After(time.Second * 2)
Output: {id ONLINE} {id OFFLINE}
func (*Redis) ListenStatusChanges ¶
ListenStatusChanges subscribes with a pattern to the redis and gets online and offline status changes from it
func (*Redis) Online ¶
Online resets the expiration time for any given key. If key doesnt exists, it means key (user) become online and should be set as online. Whenever application gets any probe from a client should call this function. This method performs way better when there is a throttling mechanism implemented on top of it, please refer to benchmarks
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session holds the backend and provides accessor methods for communication
func New ¶
New creates a session for any broker system that is architected to use, communicate, forward events to the presence system
func (*Session) ListenStatusChanges ¶
ListenStatusChanges subscribes the backend and gets online and offline status changes from it