notifier

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2023 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServer

func NewServer(s *Supervisor) *http.Server

NewServer creates a new HTTP server for MicroNotifier.

Types

type Application

type Application struct {
	Name     string
	Channels map[string]*Channel
	Users    mapset.Set // Set of User
}

Application is a namaspece for channels

func (*Application) GetUserByID

func (a *Application) GetUserByID(ID int) *User

GetUserByID returns a user with the given ID, or nil. (NB: Expect nil return value, for the user may not be managed by this process.)

type Channel

type Channel struct {
	Name  string
	Users map[int]int // user id -> subscription count
}

Channel corresponds to Pusher channel. Channels are implicitly created when subscribed and/or triggered.

func (*Channel) SubscribeUser

func (c *Channel) SubscribeUser(uid int) int

SubscribeUser let the user of uid subscribe the channel. Returns the number of subscribers of the channel.

func (*Channel) SubscriptionCount

func (c *Channel) SubscriptionCount() int

SubscriptionCount returns the number of subscriptions of the channel. A user can subscribe the same channel multiple times, so the count can be greater than UserCount.

func (*Channel) UnsubscribeUser

func (c *Channel) UnsubscribeUser(uid int) int

UnsubscribeUser let the user of uid unsubscribe the channel. Returns the updated number of subscribers of the channel.

func (*Channel) UserCount

func (c *Channel) UserCount() int

UserCount returns the number of users subscribing the channel.

type Config

type Config struct {
	Host         string              `json:"host"`
	Port         int                 `json:"port"`
	Certificate  string              `json:"certificate"`
	PrivateKey   string              `json:"private-key"`
	Redis        ConfigRedis         `json:"redis"`
	Applications []ConfigApplication `json:"applications"`
}

Config holds the enture configuration parameters.

func ReadConfig

func ReadConfig(r io.Reader) (*Config, error)

ReadConfig reads configuration data and returns Config struct or an error.

func ReadConfigFile

func ReadConfigFile(file string) (*Config, error)

ReadConfigFile reads a config file and returns Config struct or an error.

func (*Config) GetApp

func (c *Config) GetApp(name string) *ConfigApplication

GetApp extracts ConfigApplication of the named application, or nil if no such application is defined in the config.

func (*Config) GetAppFromKey

func (c *Config) GetAppFromKey(key string) *ConfigApplication

GetAppFromKey extracts ConfigApplication of the application with given key, or nil if no such application is defined in the config.

type ConfigApplication

type ConfigApplication struct {
	Name   string `json:"name"`
	Key    string `json:"key"`
	Secret string `json:"secret"`
}

ConfigApplication is the configuration of individual applications.

type ConfigError

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

ConfigError will be returned when something bad occur during reading config file.

func (*ConfigError) Error

func (e *ConfigError) Error() string

func (*ConfigError) Unwrap

func (e *ConfigError) Unwrap() error

Unwrap returns inner error

type ConfigRedis

type ConfigRedis struct {
	Address  string `json:"address"`
	Database int    `json:"database"`
	Password string `json:"password"`
	Sentinel bool   `json:"sentinel"`
	Secure   bool   `json:"secure"`
}

ConfigRedis is an optional Redis configuration parameters.

type ConnectionEstablishedData

type ConnectionEstablishedData struct {
	SocketID        string `json:"socket_id"`
	ActivityTimeout int    `json:"activity_timeout"`
}

ConnectionEstablishedData is a struct to return pusher:connection_established event to the client.

type DB

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

DB encapsulates Redis operation from other parts

func InitDB

func InitDB(config *Config) *DB

InitDB establishes connections to Redis server according to the config parameters.

func (*DB) AddUserIDToChannel

func (db *DB) AddUserIDToChannel(appname string, channame string, uid int) error

AddUserIDToChannel adds UID to the list of subscribers in the specified channel.

func (*DB) DeleteUserID

func (db *DB) DeleteUserID(appname string, uid int) error

DeleteUserID deletes the given user id. Note: The user must have been unsubscribed from all the channels. Supervisor.RemoveUser takes care of that.

func (*DB) DeleteUserIDFromChannel

func (db *DB) DeleteUserIDFromChannel(appname string, channame string, uid int) error

DeleteUserIDFromChannel removes the given uid from the subscribers of the specified channel.

func (*DB) FinishDB

func (db *DB) FinishDB()

FinishDB cleans up Redis connection

func (*DB) FlushDB

func (db *DB) FlushDB()

FlushDB flushes Redis commands

func (*DB) GetAllUserIDs

func (db *DB) GetAllUserIDs(appname string) ([]int, error)

GetAllUserIDs returns all user IDs in the given app.

func (*DB) GetChannel

func (db *DB) GetChannel(appname string, channame string) (*Channel, error)

GetChannel returns the named channel. The named channel must exist.

func (*DB) GetChannels

func (db *DB) GetChannels(appname string) (map[string]*Channel, error)

GetChannels returns map of channel names to channels

func (*DB) GetOrCreateChannel

func (db *DB) GetOrCreateChannel(appname string, channame string) (*Channel, error)

GetOrCreateChannel returns the named channel; if the named channel doesn't exist, create one.

type Event

type Event struct {
	Name string
	Data string
}

Event is the actual event to be sent.

type EventRequest

type EventRequest struct {
	Name        string // event name
	Data        string // event payload
	Application string // application name
	Channel     string // target channel name
}

EventRequest is a packet dispersed via pubsub channel

type PusherEvent

type PusherEvent struct {
	Event   string `json:"event"`
	Data    string `json:"data"`
	Channel string `json:"channel",omitempty`
}

PusherEvent is a struct to receive Pusher message. See https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol#events

type Supervisor

type Supervisor struct {
	Port   int
	Apps   []*Application
	Config *Config
	// contains filtered or unexported fields
}

Supervisor is the root of runtime data structure.

func NewSupervisor

func NewSupervisor(config *Config) *Supervisor

NewSupervisor creates a new Supervisor.

func (*Supervisor) AddUser

func (s *Supervisor) AddUser(appname string, conn *websocket.Conn) (*User, error)

AddUser creates a new user associated to an application, with the given connection.

func (*Supervisor) Broadcast

func (s *Supervisor) Broadcast(a *Application, e *Event, cn string) error

Broadcast sends out the event to the users who subscribe the given channel. In distributed mode, we don't know which process is managing the user, so we use Redis Keyspace Notification.

func (*Supervisor) Finish

func (s *Supervisor) Finish()

Finish finalizes the Supervisor.

func (*Supervisor) GetApp

func (s *Supervisor) GetApp(name string) (*Application, error)

GetApp returns the named application.

func (*Supervisor) GetAppFromKey

func (s *Supervisor) GetAppFromKey(key string) (*Application, error)

GetAppFromKey returns the application with specified key.

func (*Supervisor) GetChannel

func (s *Supervisor) GetChannel(appname string, channame string) (*Channel, error)

GetChannel returns the named channel in the named application. If there's no such channel, 404 error is returned.

func (*Supervisor) GetChannels

func (s *Supervisor) GetChannels(appname string) (map[string]*Channel, error)

GetChannels returns an array of channels in the given app

func (*Supervisor) GetOrCreateChannel

func (s *Supervisor) GetOrCreateChannel(appname string, channame string) (*Channel, error)

GetOrCreateChannel returns the named channel in the named application. If there's no such channel, create it.

func (*Supervisor) InitApps

func (s *Supervisor) InitApps()

InitApps populates Supervisor.Apps.

func (*Supervisor) KickRedisSubscription

func (s *Supervisor) KickRedisSubscription()

KickRedisSubscription starts goroutine to handle Redis events.

func (*Supervisor) PublishRedisEvent

func (s *Supervisor) PublishRedisEvent(ev *EventRequest) error

PublishRedisEvent pushes Redis events.

func (*Supervisor) RemoveUser

func (s *Supervisor) RemoveUser(appname string, uid int) error

RemoveUser removes the specified user from the application and associated channels. NB: The user's connection must be cleaned up by the caller (see socket.go)

func (*Supervisor) Subscribe

func (s *Supervisor) Subscribe(appname string, uid int, channame string) error

Subscribe let the user subscribe the named channel The user with UID must be managed by this process (when socket.go calls this, it should.)

func (*Supervisor) Unsubscribe

func (s *Supervisor) Unsubscribe(appname string, uid int, channame string) error

Unsubscribe let the user unsubscribe the named channel The user with UID must be managed by this process (when socket.go calls this, it should.)

type UIDArray

type UIDArray struct {
	UIDs []int
}

UIDArray is kept in <application>/users

type User

type User struct {
	ID         int
	Connection *websocket.Conn
	App        *Application
	SocketID   string
}

User hold individial user (assigned with unique ID)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL