hub

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Websocket message broker

This project is a message broker for websocket connections.

Motivation

Needed a way to handle the connection management for websockets from an API.

gopherbadger-tag-do-not-edit

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogLevel string = "DEBUG"

LogLevel is the default logging level

Functions

This section is empty.

Types

type ActionMessage

type ActionMessage struct {
	Action  string      `json:"action"`
	Payload interface{} `json:"payload"`
}

ActionMessage is the message from the network

type Broker

type Broker struct {
	Hub
}

Broker is the application structure

func NewBroker

func NewBroker(origins []string, opts ...BrokerOption) *Broker

NewBroker is a Broker constructor which instantiates the Hub.

type BrokerOption

type BrokerOption func(*Broker)

func WithNotifier

func WithNotifier(n Notifier) BrokerOption

type Client

type Client struct {
	ID string

	Topics []string
	// contains filtered or unexported fields
}

Client represents a single connection from a user.

func NewClient

func NewClient(ws *clientServerWS, h *Hub, ID string) *Client

NewClient creates a new client.

func (*Client) AddTopic

func (c *Client) AddTopic(topic string)

AddTopic adds a topic to a client.

func (*Client) ClearTopics

func (c *Client) ClearTopics()

RemoveTopic removes topics from a client and the client from hub's topic map

func (*Client) Subscribe

func (c *Client) Subscribe(topic string)

Subscribe subscribes a client to a topic.

func (*Client) SubscribeMultiple

func (c *Client) SubscribeMultiple(topics []string)

SubscribeMultiple subscribes the client to multiple topics.

type ConnMessage

type ConnMessage struct {
	AuthID          string `json:"AuthID"`
	AccessToken     string `json:"access_token"`
	ImpersonationID int    `json:"impersonationID"`
	Topics          []string
}

type Hub

type Hub struct {

	// protects connections
	sync.Mutex

	OnSubscribe func(*Subscription)
	// contains filtered or unexported fields
}

Hub contains the active clients with thread-safe connection management, handles subscriptions and broadcasts messages to the clients.

func NewHub

func NewHub(logOutput io.Writer, origins []string) *Hub

NewHub Instantiates the Hub. Adds the factory design pattern as a websocket HTTP connection upgrader. Creates the broker's logger

func (*Hub) Notify

func (h *Hub) Notify(s string)

func (*Hub) Publish

func (h *Hub) Publish(m PublishMessage)

Publish sends Mailmessage m to the hub's emitter.

func (*Hub) Run

func (h *Hub) Run()

Run starts the Hub.

func (*Hub) ServeHTTP

func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP upgrades HTTP connection to a ws/wss connection. Sets up the connection and registers it to the hub for read/write operations.

type MailMessage

type MailMessage struct {
	Action   string      `json:"action"`
	Topic    string      `json:"topic"`
	SubTopic string      `json:"subtopic"`
	Message  interface{} `json:"message"`
}

type Notifier

type Notifier interface {
	Notify(s string)
}

Notifier is used to track hub channel actions. This is mainly used for testing in hub_test.go.

type PublishMessage

type PublishMessage struct {
	Topic   string `json:"topic"`
	Payload []byte `json:"payload"`
}

PublishMessage is the structure used for publishing message.

type Subscription

type Subscription struct {
	Topic  string
	Client *Client
}

Subscription represents a 1:1 relationship between topic and client.

type SubscriptionsMessage

type SubscriptionsMessage struct {
	Topics []string `json:"payload"`
}

SubscriptionsMessage is the structure used for subscriptions.

Jump to

Keyboard shortcuts

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