pubsub

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: MIT Imports: 8 Imported by: 0

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

View Source
const TwitchApiHost = "pubsub-edge.twitch.tv"
View Source
const TwitchApiMaxTopics = 50
View Source
const TwitchApiPath = ""
View Source
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

View Source
const TwitchApiPingTimeout = 10 * time.Second // 10 seconds
View Source
const TwitchApiScheme = "wss"

Default Twitch server API credentials.

https://dev.twitch.tv/docs/pubsub/#connection-management

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

func (Answer) HasError

func (a Answer) HasError() bool

func (Answer) JSON

func (a Answer) JSON() []byte

func (*Answer) Parse

func (a *Answer) Parse()

type AnswerDataMessage

type AnswerDataMessage struct {
	Message string `json:"message"`
	Topic   string `json:"topic"`
}

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 New

func New() *PubSub

New create and returns new API client.

func NewWithURL

func NewWithURL(url url.URL) *PubSub

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

func (p *PubSub) HasTopic(topic string, params ...interface{}) bool

HasTopic returns true if topic present.

func (*PubSub) Listen

func (p *PubSub) Listen(ctx context.Context, topic string, params ...interface{})

Listen is adding topics for listening. It take care of API limits. New TCP connection will be created for every 50 topics.

https://dev.twitch.tv/docs/pubsub/#connection-management

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.

func (*PubSub) OnPong

func (c *PubSub) OnPong(fn func(*Connection, time.Time, time.Time))

OnPong is bind func to event. Will fire for every connection.

func (*PubSub) Topic

func (p *PubSub) Topic(topic string, params ...interface{}) string

Topic generate correct topic for API. Params can be as number or string.

https://dev.twitch.tv/docs/pubsub/#topics

func (*PubSub) Topics

func (p *PubSub) Topics() []string

Topics returns all current listen topics.

func (*PubSub) TopicsCount

func (p *PubSub) TopicsCount() int

TopicsCount return count of topics.

func (*PubSub) Unlisten

func (p *PubSub) Unlisten(ctx context.Context, topic string, params ...interface{})

Unlisten is remove topics from listening. It take care of API limits too. Connection count will automatically decrease of needs.

https://dev.twitch.tv/docs/pubsub/#connection-management

Jump to

Keyboard shortcuts

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