Documentation ¶
Index ¶
- Constants
- Variables
- func ParseTopic(str string, args ...interface{}) string
- type Client
- func (client *Client) Close()
- func (client *Client) GetNextShard() (*Conn, error)
- func (client *Client) GetNumShards() int
- func (client *Client) GetNumTopics() (n int)
- func (client *Client) GetShard(id int) (*Conn, error)
- func (client *Client) Listen(topic string, args ...interface{}) error
- func (client *Client) ListenWithAuth(token string, topic string, args ...interface{}) error
- func (client *Client) OnShardConnect(f func(int))
- func (client *Client) OnShardDisconnect(f func(int))
- func (client *Client) OnShardLatencyUpdate(f func(int, time.Duration))
- func (client *Client) OnShardMessage(f func(int, string, []byte))
- func (client *Client) OnShardReconnect(f func(int))
- func (client *Client) SetMaxShards(max int)
- func (client *Client) SetMaxTopicsPerShard(max int)
- func (client *Client) Unlisten(topics ...string) error
- type Conn
- func (conn *Conn) Close()
- func (conn *Conn) Connect() error
- func (conn *Conn) GetNumTopics() (n int)
- func (conn *Conn) HasTopic(topic string) bool
- func (conn *Conn) IsConnected() bool
- func (conn *Conn) Listen(topics ...string) error
- func (conn *Conn) ListenWithAuth(token string, topics ...string) error
- func (conn *Conn) OnDisconnect(f func())
- func (conn *Conn) OnMessage(f func(string, []byte))
- func (conn *Conn) OnPong(f func(time.Duration))
- func (conn *Conn) OnReconnect(f func())
- func (conn *Conn) Ping() (time.Duration, error)
- func (conn *Conn) Reconnect() error
- func (conn *Conn) SetMaxTopics(max int)
- func (conn *Conn) SetNonceGenerator(gen NonceGenerator) error
- func (conn *Conn) Unlisten(topics ...string) error
- func (conn *Conn) Write(msgType int, data []byte) error
- func (conn *Conn) WriteMessage(msgType MessageType, data interface{}) error
- func (conn *Conn) WriteMessageWithNonce(msgType MessageType, nonce string, data interface{}) error
- type IClient
- type IConn
- type MessageData
- type MessageError
- type MessageType
- type NonceGenerator
- type Packet
- type TopicData
Constants ¶
const IP = "pubsub-edge.twitch.tv"
IP for the PubSub server
Variables ¶
var ( // ErrShardTooManyTopics returned when a shard has attempted to join too many topics ErrShardTooManyTopics = errors.New("too many topics on shard") // ErrShardIDOutOfBounds returned when an invalid shard id is provided ErrShardIDOutOfBounds = errors.New("shard id out of bounds") // ErrNonceTimeout returned when the server doesnt respond to a nonced message in time ErrNonceTimeout = errors.New("nonced message timeout") // ErrPingTimeout returned when the server takes too long to respond to a ping message ErrPingTimeout = errors.New("server took too long to respond to ping") // ErrBadMessage returned when the server receives an invalid message ErrBadMessage = errors.New("server received an invalid message") // ErrBadAuth returned when a topic doesnt have the permissions required ErrBadAuth = errors.New("bad authentication for topic") // ErrBadTopic returned when an invalid topic was requested ErrBadTopic = errors.New("invalid topic") // ErrServer returned when something went wrong on the servers end ErrServer = errors.New("something went wrong on the servers end") // ErrUnknown returned when the server sends back an error that wasnt handled by the reader ErrUnknown = errors.New("server sent back an unknown error") // ErrInvalidNonceGenerator returned when a provided nonce generator can not be used ErrInvalidNonceGenerator = errors.New("nonce generator is invalid") )
Functions ¶
func ParseTopic ¶
ParseTopic returns a topic string with the provided arguments
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client stores data about a PubSub shard manager
func New ¶
func New() *Client
New PubSub Client
The client uses a sharding system to comply with limits as listed on the Twitch PubSub Documentation. Twitch recommends no more than 10 simultaneous shards and no more than 50 topics per shard. These are set by default.
If for any reason a shard attempts to listen to more topics than the server allows, it will attempt to correct for it.
func (*Client) GetNextShard ¶
GetNextShard returns the first shard that can accept topics
func (*Client) GetNumShards ¶
GetNumShards returns the number of active shards
func (*Client) GetNumTopics ¶
GetNumTopics returns the number of topics being listened to across all shards
func (*Client) ListenWithAuth ¶
ListenWithAuth starts listening to a topic on the best available shard using the provided authentication token
func (*Client) OnShardConnect ¶
OnShardConnect event called after a shard connects to the PubSub server
func (*Client) OnShardDisconnect ¶
OnShardDisconnect event called after a shard is disconnected from the PubSub server
func (*Client) OnShardLatencyUpdate ¶
OnShardLatencyUpdate event called after a shards latency is updated
func (*Client) OnShardMessage ¶
OnShardMessage event called after a shard gets a PubSub message
func (*Client) OnShardReconnect ¶
OnShardReconnect event called after a shard reconnects to the PubSub server
func (*Client) SetMaxTopicsPerShard ¶
SetMaxTopicsPerShard set the maximum number of topics for each shard
Default: 50
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn stores data about a PubSub connection
func (*Conn) GetNumTopics ¶
GetNumTopics returns the number of topics the connection is actively listening to
func (*Conn) HasTopic ¶
HasTopic returns true if the connection is actively listening to the provided topic
func (*Conn) IsConnected ¶
IsConnected returns true if the socket is actively connected
func (*Conn) Listen ¶
Listen to a topic using no authentication token
This operation will block, giving the server up to 5 seconds to respond after correcting for latency before failing
func (*Conn) ListenWithAuth ¶
ListenWithAuth starts listening to a topic using the provided authentication token
This operation will block, giving the server up to 5 seconds to respond after correcting for latency before failing
func (*Conn) OnDisconnect ¶
func (conn *Conn) OnDisconnect(f func())
OnDisconnect event called after the connection is closed
func (*Conn) OnReconnect ¶
func (conn *Conn) OnReconnect(f func())
OnReconnect event called after the connection is reopened
func (*Conn) Ping ¶
Ping the PubSub server
This operation will block, giving the server up to 5 seconds to respond after correcting for latency before failing
func (*Conn) SetMaxTopics ¶
SetMaxTopics changes the maximum number of topics the connection can listen to
func (*Conn) SetNonceGenerator ¶
func (conn *Conn) SetNonceGenerator(gen NonceGenerator) error
SetNonceGenerator changes the nonce generator that will be used
A valid NonceGenerator is a function that takes no arguments and returns a string that is different every time it is called. Nonce strings must be at least 5 characters long.
func (*Conn) Unlisten ¶
Unlisten from the provided topics
This operation will block, giving the server up to 5 seconds to respond after correcting for latency before failing
func (*Conn) WriteMessage ¶
func (conn *Conn) WriteMessage(msgType MessageType, data interface{}) error
WriteMessage with no nonce and send it to the server
func (*Conn) WriteMessageWithNonce ¶
func (conn *Conn) WriteMessageWithNonce(msgType MessageType, nonce string, data interface{}) error
WriteMessageWithNonce write a message with the provided nonce and send it to the server
This operation will block, giving the server up to 5 seconds to respond after correcting for latency before failing
type IClient ¶
type IClient interface { SetMaxShards(int) SetMaxTopicsPerShard(int) GetNumShards() int GetNumTopics() int GetNextShard() (*Conn, error) GetShard(int) (*Conn, error) Close() Listen(string, ...interface{}) error ListenWithAuth(string, string, ...interface{}) error Unlisten(...string) error OnShardConnect(func(int)) OnShardMessage(func(int, string, []byte)) OnShardLatencyUpdate(func(int, time.Duration)) OnShardReconnect(func(int)) OnShardDisconnect(func(int)) }
IClient interface for methods used by the PubSub shard manager
type IConn ¶
type IConn interface { Connect() error Reconnect() error Write(int, []byte) error WriteMessage(MessageType, interface{}) error WriteMessageWithNonce(MessageType, string, interface{}) error Close() IsConnected() bool SetNonceGenerator(NonceGenerator) error SetMaxTopics(int) GetNumTopics() int HasTopic(string) bool Listen(...string) error ListenWithAuth(string, ...string) error Unlisten(...string) error Ping() (time.Duration, error) OnMessage(func(string, []byte)) OnPong(func(time.Duration)) OnReconnect(func()) OnDisconnect(func()) }
IConn interface for methods used by the PubSub connection
type MessageData ¶
MessageData stores data about a message packet
type MessageError ¶
type MessageError string
MessageError stores the error provided in MessageData
const ( // BadMessage server received an invalid message BadMessage MessageError = "ERR_BADMESSAGE" // BadAuth provided token does not have required permissions BadAuth MessageError = "ERR_BADAUTH" // TooManyTopics attempted to listen to too many topics TooManyTopics MessageError = "ERR_TOO_MANY_TOPICS" // BadTopic provided topic is invalid BadTopic MessageError = "ERR_BADTOPIC" // InvalidTopic provided topic is invalid InvalidTopic MessageError = "Invalid Topic" // ServerError something went wrong on the servers side ServerError MessageError = "ERR_SERVER" )
type MessageType ¶
type MessageType string
MessageType stores the type provided in MessageData
const ( // Listen outgoing message type Listen MessageType = "LISTEN" // Unlisten outgoing message type Unlisten MessageType = "UNLISTEN" // Ping outgoing message type Ping MessageType = "PING" // Response incoming message type Response MessageType = "RESPONSE" // Message incoming message type Message MessageType = "MESSAGE" // Pong incoming message type Pong MessageType = "PONG" // Reconnect incoming message type Reconnect MessageType = "RECONNECT" )
type NonceGenerator ¶
type NonceGenerator func() string
NonceGenerator any function that returns a string that is different every time
type Packet ¶
type Packet struct { Type MessageType `json:"type"` Nonce string `json:"nonce,omitempty"` Data interface{} `json:"data,omitempty"` Error MessageError `json:"error,omitempty"` }
Packet stores data about a message sent to/from the PubSub server