Documentation
¶
Overview ¶
Package implements Twitch API PubSub and automatically take care of API limits. Also it will handle automatically reconnections, ping/pong and maintenance requests.
Index ¶
- Constants
- type Answer
- type AnswerDataMessage
- type AnswerDataTopics
- type AnswerType
- type Connection
- func (c *Connection) AddTopic(topic string)
- func (c *Connection) Close() error
- func (c *Connection) HasTopic(topic string) bool
- func (c *Connection) OnConnect(fn func(*Connection))
- func (c *Connection) OnDisconnect(fn func(*Connection))
- func (c *Connection) OnError(fn func(*Connection, error))
- func (c *Connection) OnInfo(fn func(*Connection, string))
- func (c *Connection) OnMessage(fn func(*Connection, *Answer))
- func (c *Connection) OnPing(fn func(*Connection, time.Time))
- func (c *Connection) OnPong(fn func(*Connection, time.Time, time.Time))
- func (c *Connection) RemoveAllTopics()
- func (c *Connection) RemoveTopic(topic string)
- func (c *Connection) Topics() []string
- func (c *Connection) TopicsCount() int
- type PubSub
- func (p *PubSub) Close()
- func (p *PubSub) HasTopic(topic string, params ...interface{}) bool
- func (p *PubSub) Listen(ctx context.Context, topic string, params ...interface{})
- func (c *PubSub) OnConnect(fn func(*Connection))
- func (c *PubSub) OnDisconnect(fn func(*Connection))
- func (c *PubSub) OnError(fn func(*Connection, error))
- func (c *PubSub) OnInfo(fn func(*Connection, string))
- func (c *PubSub) OnMessage(fn func(*Connection, *Answer))
- func (c *PubSub) OnPing(fn func(*Connection, time.Time))
- func (c *PubSub) OnPong(fn func(*Connection, time.Time, time.Time))
- func (p *PubSub) Topic(topic string, params ...interface{}) string
- func (p *PubSub) Topics() []string
- func (p *PubSub) TopicsCount() int
- func (p *PubSub) Unlisten(ctx context.Context, topic string, params ...interface{})
Constants ¶
const TwitchApiHost = "pubsub-edge.twitch.tv"
const TwitchApiMaxTopics = 50
const TwitchApiPath = ""
const TwitchApiPingEach = 15 * time.Second // 30 seconds
At least once every 5 minutes by docs but better keep this at 30 seconds
https://dev.twitch.tv/docs/pubsub/#connection-management
const TwitchApiPingTimeout = 10 * time.Second // 10 seconds
const TwitchApiScheme = "wss"
Default Twitch server API credentials.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Answer ¶
type Answer struct { Type AnswerType `json:"type"` Data any `json:"data,omitempty"` Error string `json:"error,omitempty"` Nonce string `json:"nonce,omitempty"` // contains filtered or unexported fields }
func (*Answer) GetData ¶
func (a *Answer) GetData() AnswerDataMessage
type AnswerDataMessage ¶
func (AnswerDataMessage) JSON ¶
func (a AnswerDataMessage) JSON() []byte
type AnswerDataTopics ¶
type AnswerDataTopics struct {
Topics []string `json:"topics"`
}
func (AnswerDataTopics) JSON ¶
func (a AnswerDataTopics) JSON() []byte
type AnswerType ¶
type AnswerType string
const ( Listen AnswerType = "LISTEN" Message AnswerType = "MESSAGE" Ping AnswerType = "PING" Pong AnswerType = "PONG" Reconnect AnswerType = "RECONNECT" Response AnswerType = "RESPONSE" Unlisten AnswerType = "UNLISTEN" )
func (AnswerType) String ¶
func (a AnswerType) String() string
type Connection ¶
type Connection struct { sync.RWMutex ID int64 Connection *websocket.Conn // contains filtered or unexported fields }
Connection is represent of one connection.
func NewConnection ¶
func NewConnection(url url.URL) *Connection
NewConnection create new connection. Returns pointer to connection.
func (*Connection) AddTopic ¶
func (c *Connection) AddTopic(topic string)
AddTopic is adding topics for listening.
func (*Connection) Close ¶
func (c *Connection) Close() error
Close is close connection and shutdown all goroutines. Usually it's need to call before destroying.
func (*Connection) HasTopic ¶
func (c *Connection) HasTopic(topic string) bool
HasTopic returns true if topic present.
func (*Connection) OnConnect ¶
func (c *Connection) OnConnect(fn func(*Connection))
func (*Connection) OnDisconnect ¶
func (c *Connection) OnDisconnect(fn func(*Connection))
func (*Connection) OnError ¶
func (c *Connection) OnError(fn func(*Connection, error))
func (*Connection) OnInfo ¶
func (c *Connection) OnInfo(fn func(*Connection, string))
func (*Connection) OnMessage ¶
func (c *Connection) OnMessage(fn func(*Connection, *Answer))
func (*Connection) OnPing ¶
func (c *Connection) OnPing(fn func(*Connection, time.Time))
func (*Connection) OnPong ¶
func (c *Connection) OnPong(fn func(*Connection, time.Time, time.Time))
func (*Connection) RemoveAllTopics ¶
func (c *Connection) RemoveAllTopics()
RemoveAllTopics is remove all topics from listening.
func (*Connection) RemoveTopic ¶
func (c *Connection) RemoveTopic(topic string)
RemoveTopic is remove topic from listening.
func (*Connection) Topics ¶
func (c *Connection) Topics() []string
Topics returns all current listen topics.
func (*Connection) TopicsCount ¶
func (c *Connection) TopicsCount() int
TopicsCount return count of topics.
type PubSub ¶
type PubSub struct { sync.RWMutex URL url.URL Connections map[int64]*Connection // contains filtered or unexported fields }
PubSub is represent of API client.
func NewWithURL ¶
NewWithURL create and returns new API client with custom API server URL. It can be useful for testing.
func (*PubSub) Close ¶
func (p *PubSub) Close()
Close is close all connections. Usually need to call at the end of app life.
func (*PubSub) Listen ¶
Listen is adding topics for listening. It take care of API limits. New TCP connection will be created for every 50 topics.
func (*PubSub) OnConnect ¶
func (c *PubSub) OnConnect(fn func(*Connection))
OnConnect is bind func to event. Will fire for every connection.
func (*PubSub) OnDisconnect ¶
func (c *PubSub) OnDisconnect(fn func(*Connection))
OnDisconnect is bind func to event. Will fire for every connection.
func (*PubSub) OnError ¶
func (c *PubSub) OnError(fn func(*Connection, error))
OnError is bind func to event. Will fire for every connection.
func (*PubSub) OnInfo ¶
func (c *PubSub) OnInfo(fn func(*Connection, string))
OnInfo is bind func to event. Will fire for every connection.
func (*PubSub) OnMessage ¶
func (c *PubSub) OnMessage(fn func(*Connection, *Answer))
OnMessage is bind func to event. Will fire for every connection.
func (*PubSub) OnPing ¶
func (c *PubSub) OnPing(fn func(*Connection, time.Time))
OnPing is bind func to event. Will fire for every connection.