Documentation ¶
Index ¶
- Constants
- type Broker
- type Client
- type Command
- type CommandHandler
- type Config
- type ConnContext
- type ErrorEvent
- type Memory
- type Message
- type Redis
- type RedisConfig
- type Service
- func (s *Service) AddCommand(name string, cmd CommandHandler)
- func (s *Service) AddListener(l func(event int, ctx interface{}))
- func (s *Service) Broadcast(msg ...*Message) error
- func (s *Service) Broker() Broker
- func (s *Service) Init(cfg *Config, r *rpc.Service, h *rhttp.Service, e env.Environment) (bool, error)
- func (s *Service) NewClient(upstream chan *Message) *Client
- func (s *Service) Serve() (err error)
- func (s *Service) Stop()
- func (s *Service) Subscribe(upstream chan *Message, topics ...string) error
- func (s *Service) Unsubscribe(upstream chan *Message, topics ...string)
- type TopicEvent
Constants ¶
const ( // EventWebsocketConnect fired when new client is connected, the context is *websocket.Conn. EventWebsocketConnect = iota + 2500 // EventWebsocketDisconnect fired when websocket is disconnected, context is empty. EventWebsocketDisconnect // EventWebsocketJoin caused when topics are being consumed, context if *TopicEvent. EventWebsocketJoin // EventWebsocketLeave caused when topic consumption are stopped, context if *TopicEvent. EventWebsocketLeave // EventWebsocketError when any broadcast error occurred, the context is *ErrorEvent. EventWebsocketError // EventBrokerError the context is error. EventBrokerError )
const ID = "broadcast"
ID defines public service name.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker interface { // Serve serves broker. Serve() error // close the consumption and disconnect broker. Stop() // Subscribe broker to one or multiple topics. Subscribe(upstream chan *Message, topics ...string) error // Unsubscribe broker from one or multiple topics. Unsubscribe(upstream chan *Message, topics ...string) // Broadcast one or multiple Messages. Broadcast(messages ...*Message) error }
Broker defines the ability to operate as message passing broker.
type Client ¶ added in v1.1.2
type Client struct {
// contains filtered or unexported fields
}
NewClient subscribes to a given topic and consumes or publish messages to it. NewClient will be receiving messages it produced.
func (*Client) Unsubscribe ¶ added in v1.1.2
Unsubscribe client from specific topics
type Command ¶
type Command struct { // Cmd type. Cmd string `json:"cmd"` // Args contains command specific payload. Args json.RawMessage `json:"args"` }
Command contains information send by user.
type CommandHandler ¶
type CommandHandler func(ctx *ConnContext, cmd []byte)
CommandHandler handles custom commands.
type Config ¶
type Config struct { // Path defines on this URL the middleware must be activated. Same path must be handled by underlying // application kernel to authorize the consumption. Optional. Path string // RedisConfig configures redis broker. Redis *RedisConfig }
Config configures the broadcast extension.
func (*Config) InitDefaults ¶ added in v1.1.2
InitDefaults enables in memory broadcast configuration.
type ConnContext ¶
type ConnContext struct { // Upstream to push Messages into. Upstream chan *Message // Conn to the client. Conn *websocket.Conn // Topics contain list of currently subscribed topics. Topics []string }
ConnContext represents the connection and it's state.
type ErrorEvent ¶
type ErrorEvent struct { // Conn specific to the error. Conn *websocket.Conn // Caused contains job specific error. Caused error }
ErrorEvent represents singular broadcast error event.
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory manages broadcasting in memory.
func (*Memory) Unsubscribe ¶
Unsubscribe broker from one or multiple channels.
type Message ¶
type Message struct { // Topic message been pushed into. Topic string `json:"topic"` // Payload to be broadcasted. Must be valid JSON. Payload json.RawMessage `json:"payload"` }
Message represent single message.
func NewMessage ¶
NewMessage creates new message with JSON payload.
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis based broadcast router.
func (*Redis) Unsubscribe ¶
Unsubscribe broker from one or multiple channels.
type RedisConfig ¶
RedisConfig configures redis broker.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service manages even broadcasting and websocket interface.
func (*Service) AddCommand ¶
func (s *Service) AddCommand(name string, cmd CommandHandler)
AddCommand attached custom client command handler, for websocket only.
func (*Service) AddListener ¶
AddListener attaches server event controller.
func (*Service) Init ¶
func (s *Service) Init(cfg *Config, r *rpc.Service, h *rhttp.Service, e env.Environment) (bool, error)
Init service.
func (*Service) NewClient ¶ added in v1.1.2
NewClient returns single connected client with ability to consume or produce into topic.
func (*Service) Unsubscribe ¶ added in v1.1.0
Unsubscribe broker from one or multiple topics.
type TopicEvent ¶
type TopicEvent struct { // Conn associated with topics. Conn *websocket.Conn // Topics specific to event. Topics []string }
TopicEvent caused when topic is joined or left.