pubsub

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: BSD-3-Clause Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const IP = "pubsub-edge.twitch.tv"

IP for the PubSub server

Variables

View Source
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

func ParseTopic(str string, args ...interface{}) string

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.

See: https://dev.twitch.tv/docs/pubsub

func (*Client) Close

func (client *Client) Close()

Close all active shards

func (*Client) GetNextShard

func (client *Client) GetNextShard() (*Conn, error)

GetNextShard returns the first shard that can accept topics

func (*Client) GetNumShards

func (client *Client) GetNumShards() int

GetNumShards returns the number of active shards

func (*Client) GetNumTopics

func (client *Client) GetNumTopics() (n int)

GetNumTopics returns the number of topics being listened to across all shards

func (*Client) GetShard

func (client *Client) GetShard(id int) (*Conn, error)

GetShard retrieves or creates a shard based on the provided id

func (*Client) Listen

func (client *Client) Listen(topic string, args ...interface{}) error

Listen to a topic on the best available shard

func (*Client) ListenWithAuth

func (client *Client) ListenWithAuth(token string, topic string, args ...interface{}) error

ListenWithAuth starts listening to a topic on the best available shard using the provided authentication token

func (*Client) OnShardConnect

func (client *Client) OnShardConnect(f func(int))

OnShardConnect event called after a shard connects to the PubSub server

func (*Client) OnShardDisconnect

func (client *Client) OnShardDisconnect(f func(int))

OnShardDisconnect event called after a shard is disconnected from the PubSub server

func (*Client) OnShardLatencyUpdate

func (client *Client) OnShardLatencyUpdate(f func(int, time.Duration))

OnShardLatencyUpdate event called after a shards latency is updated

func (*Client) OnShardMessage

func (client *Client) OnShardMessage(f func(int, string, []byte))

OnShardMessage event called after a shard gets a PubSub message

func (*Client) OnShardReconnect

func (client *Client) OnShardReconnect(f func(int))

OnShardReconnect event called after a shard reconnects to the PubSub server

func (*Client) SetMaxShards

func (client *Client) SetMaxShards(max int)

SetMaxShards set the maximum number of shards

Default: 10

func (*Client) SetMaxTopicsPerShard

func (client *Client) SetMaxTopicsPerShard(max int)

SetMaxTopicsPerShard set the maximum number of topics for each shard

Default: 50

func (*Client) Unlisten

func (client *Client) Unlisten(topics ...string) error

Unlisten from the provided topics

Will return the first error that occurs, if any

type Conn

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

Conn stores data about a PubSub connection

func (*Conn) Close

func (conn *Conn) Close()

Close the connection to the PubSub server

func (*Conn) Connect

func (conn *Conn) Connect() error

Connect to the PubSub server

func (*Conn) GetNumTopics

func (conn *Conn) GetNumTopics() (n int)

GetNumTopics returns the number of topics the connection is actively listening to

func (*Conn) HasTopic

func (conn *Conn) HasTopic(topic string) bool

HasTopic returns true if the connection is actively listening to the provided topic

func (*Conn) IsConnected

func (conn *Conn) IsConnected() bool

IsConnected returns true if the socket is actively connected

func (*Conn) Listen

func (conn *Conn) Listen(topics ...string) error

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

func (conn *Conn) ListenWithAuth(token string, topics ...string) error

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

func (conn *Conn) OnMessage(f func(string, []byte))

OnMessage event called after a message is receieved

func (*Conn) OnPong

func (conn *Conn) OnPong(f func(time.Duration))

OnPong event called after a Pong message is received, updating the latency

func (*Conn) OnReconnect

func (conn *Conn) OnReconnect(f func())

OnReconnect event called after the connection is reopened

func (*Conn) Ping

func (conn *Conn) Ping() (time.Duration, error)

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

func (conn *Conn) Reconnect() error

Reconnect to the PubSub server

func (*Conn) SetMaxTopics

func (conn *Conn) SetMaxTopics(max int)

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

func (conn *Conn) Unlisten(topics ...string) error

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

func (conn *Conn) Write(msgType int, data []byte) error

Write a message and send it to the server

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

type MessageData struct {
	Topic string `json:"topic"`
	Data  string `json:"message"`
}

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

type TopicData

type TopicData struct {
	Topics []string `json:"topics"`
	Token  string   `json:"auth_token,omitempty"`
}

TopicData stores data about a topic

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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