presence: github.com/cihangir/presence Index | Examples | Files

package presence

import "github.com/cihangir/presence"

Package presence provides an advanced presence system

Index

Examples

Package Files

error.go presence.go redis.go

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")
)

type Backend Uses

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

func NewRedis Uses

func NewRedis(
    server string,
    db int,
    inactiveDuration time.Duration,
) (Backend, error)

NewRedis creates a Redis presence system

type Error Uses

type Error map[string]error

Error holds the non-thread safe errors for given specific ids

func (*Error) Append Uses

func (m *Error) Append(id string, err error)

Append adds an error to the aggregated errors with an id

func (*Error) Each Uses

func (m *Error) Each(f func(id string, err error))

Each iterates over error set with calling the given function

func (Error) Error Uses

func (m Error) Error() string

Error implements the error interface

func (*Error) Has Uses

func (m *Error) Has(id string) bool

Has checks if the Error has an error for the given id

func (*Error) Len Uses

func (m *Error) Len() int

Len returns the registered error count

type Event Uses

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 Uses

type Redis struct {
    // contains filtered or unexported fields
}

Redis holds the required connection data for redis

Code:

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) Close Uses

func (s *Redis) Close() error

Close closes the redis connection gracefully

func (*Redis) Error Uses

func (s *Redis) Error() chan error

Error returns error if it happens while listening to status changes

func (*Redis) ListenStatusChanges Uses

func (s *Redis) ListenStatusChanges() chan Event

ListenStatusChanges subscribes with a pattern to the redis and gets online and offline status changes from it

func (*Redis) Offline Uses

func (s *Redis) Offline(ids ...string) error

Offline sets given ids as offline

func (*Redis) Online Uses

func (s *Redis) Online(ids ...string) error

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

func (*Redis) Status Uses

func (s *Redis) Status(ids ...string) ([]Event, error)

Status returns the current status of multiple keys from system

type Session Uses

type Session struct {
    // contains filtered or unexported fields
}

Session holds the backend and provides accessor methods for communication

func New Uses

func New(backend Backend) (*Session, error)

New creates a session for any broker system that is architected to use, communicate, forward events to the presence system

func (*Session) Close Uses

func (s *Session) Close() error

Close closes the backend connection gracefully

func (*Session) Error Uses

func (s *Session) Error() chan error

Error returns error if it happens while listening to status changes

func (*Session) ListenStatusChanges Uses

func (s *Session) ListenStatusChanges() chan Event

ListenStatusChanges subscribes the backend and gets online and offline status changes from it

func (*Session) Offline Uses

func (s *Session) Offline(ids ...string) error

Offline sets given ids as offline

func (*Session) Online Uses

func (s *Session) Online(ids ...string) error

Online sets given ids as online

func (*Session) Status Uses

func (s *Session) Status(ids ...string) ([]Event, error)

Status returns the current status of multiple keys from system

type Status Uses

type Status int

Status defines what is the current status of a user in presence system

const (
    // Unknown is for errored requests
    Unknown Status = iota

    // Offline is for displaying user as offline in the system
    Offline // do not handle unset variable as offline

    // Online is for displaying user as online in the system
    Online
)

func (Status) String Uses

func (s Status) String() string

String implements the Stringer interface

Package presence imports 8 packages (graph). Updated 2016-08-02. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).