pubsub

package
v0.0.0-...-c327406 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPubSub

type IPubSub interface {
	AddSubscriber(conn *SubscriberConnectionOpts) *Subscriber
	RemoveSubscriber(conn *SubscriberConnectionOpts) *Subscriber
	CountTopicSubscribers(topic string) int
	Sub(topics []string, conn *SubscriberConnectionOpts)
	Unsub(topics []string, conn *SubscriberConnectionOpts)
	Publish(topic []string, msg string)
	Broadcast(msg string)
}

IPubSub represents abstract PubSub for topic notifications

type ISubscriber

type ISubscriber interface {
	GetSubID() string
	GetID() string
	GetUID() string
	AddTopic(topic string)
	AddTopics(topics []string)
	RemoveTopic(topic string)
	GetTopics() []string
	Destroy()
	Notify(msg *Message)
	Listen()
}

ISubscriber represents topic(-s) subscriber

type Message

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

Message represent text notification for topic

func NewMessage

func NewMessage(topic, payload string) *Message

NewMessage create new text message for topic

func (Message) GetMessagePayload

func (m Message) GetMessagePayload() string

GetMessagePayload return message payload

func (Message) GetTopic

func (m Message) GetTopic() string

GetTopic return topic name

type OnNotifyFn

type OnNotifyFn = func(conn *SubscriberConnectionOpts, msg *Message)

OnNotifyFn callback func for notifications

type PubSub

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

PubSub implement abstraction for notifications

func NewPubSub

func NewPubSub() *PubSub

NewPubSub create default PubSub

func (*PubSub) AddSubscriber

func (b *PubSub) AddSubscriber(conn *SubscriberConnectionOpts) *Subscriber

AddSubscriber by unique id (for example, ws for connection could be "conn_id:user_id")

func (*PubSub) Broadcast

func (b *PubSub) Broadcast(msg string)

Broadcast publish msg to all

func (*PubSub) CountTopicSubscribers

func (b *PubSub) CountTopicSubscribers(topic string) int

CountTopicSubscribers count of active topic subs

func (*PubSub) Publish

func (b *PubSub) Publish(topics []string, msg string)

Publish text message to topic

func (*PubSub) RemoveSubscriber

func (b *PubSub) RemoveSubscriber(conn *SubscriberConnectionOpts) *Subscriber

RemoveSubscriber remove by unique id

func (*PubSub) Sub

func (b *PubSub) Sub(topics []string, conn *SubscriberConnectionOpts)

Sub user to topic

func (*PubSub) Unsub

func (b *PubSub) Unsub(topics []string, conn *SubscriberConnectionOpts)

Unsub user to topic

type Subscriber

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

Subscriber wrapper

func NewSubscriber

func NewSubscriber(conn *SubscriberConnectionOpts) *Subscriber

NewSubscriber create new default subscriber

func (*Subscriber) AddTopic

func (s *Subscriber) AddTopic(topic string)

AddTopic sub to topic

func (*Subscriber) AddTopics

func (s *Subscriber) AddTopics(topics []string)

AddTopics sub to topics

func (*Subscriber) Destroy

func (s *Subscriber) Destroy()

Destroy clear sub (topics, close channels)

func (*Subscriber) GetID

func (s *Subscriber) GetID() string

GetID return topics id

func (*Subscriber) GetSubID

func (s *Subscriber) GetSubID() string

GetSubID return sub id

func (*Subscriber) GetTopics

func (s *Subscriber) GetTopics() []string

GetTopics return all topics

func (*Subscriber) GetUID

func (s *Subscriber) GetUID() string

GetUID return user id

func (*Subscriber) Listen

func (s *Subscriber) Listen()

Listen incoming messages in loop

func (*Subscriber) Notify

func (s *Subscriber) Notify(msg *Message)

Notify sub with message

func (*Subscriber) RegisterNotifyHandler

func (s *Subscriber) RegisterNotifyHandler(h OnNotifyFn)

RegisterNotifyHandler attach notification handler

func (*Subscriber) RemoveTopic

func (s *Subscriber) RemoveTopic(topic string)

RemoveTopic unsub topic

type SubscriberConnectionOpts

type SubscriberConnectionOpts struct {
	CID string // ws connection uuid
	UID string // associated user id
}

SubscriberConnectionOpts represent connection options

func (*SubscriberConnectionOpts) GetSubID

func (s *SubscriberConnectionOpts) GetSubID() string

GetSubID return sub id like uuid:uid

type Subscribers

type Subscribers map[string]*Subscriber

Subscribers map of subs like map[id:Subscriber]

type Topics

type Topics map[string]Subscribers

Topics map of sub's topics like map[topic:map[id:Subscriber]]

Jump to

Keyboard shortcuts

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