Help us shape the future of Go! Take the 2026 Go Developer Survey

subscriptions

package
v0.900.9 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 8 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

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

Broker defines a struct for managing subscriptions clients.

func NewBroker

func NewBroker() *Broker

NewBroker initializes and returns a new Broker instance.

func (*Broker) ClientById

func (b *Broker) ClientById(clientId string) (Client, error)

ClientById finds a registered client by its id.

Returns non-nil error when client with clientId is not registered.

func (*Broker) Clients

func (b *Broker) Clients() map[string]Client

Clients returns a shallow copy of all registered clients indexed with their connection id.

func (*Broker) Register

func (b *Broker) Register(client Client)

Register adds a new client to the broker instance.

func (*Broker) Unregister

func (b *Broker) Unregister(clientId string)

Unregister removes a single client by its id.

If client with clientId doesn't exist, this method does nothing.

type Client

type Client interface {
	// Id Returns the unique id of the client.
	Id() string

	// Channel returns the client's communication channel.
	Channel() chan Message

	// Subscriptions returns a shallow copy of the the client subscriptions matching the prefixes.
	// If no prefix is specified, returns all subscriptions.
	Subscriptions(prefixes ...string) map[string]SubscriptionOptions

	// Subscribe subscribes the client to the provided subscriptions list.
	//
	// Each subscription can also have "options" (json serialized SubscriptionOptions) as query parameter.
	//
	// Example:
	//
	// 	Subscribe(
	// 	    "subscriptionA",
	// 	    `subscriptionB?options={"query":{"a":1},"headers":{"x_token":"abc"}}`,
	// 	)
	Subscribe(subs ...string)

	// Unsubscribe unsubscribes the client from the provided subscriptions list.
	Unsubscribe(subs ...string)

	// HasSubscription checks if the client is subscribed to `sub`.
	HasSubscription(sub string) bool

	// Set stores any value to the client's context.
	Set(key string, value any)

	// Unset removes a single value from the client's context.
	Unset(key string)

	// Get retrieves the key value from the client's context.
	Get(key string) any

	// Discard marks the client as "discarded", meaning that it
	// shouldn't be used anymore for sending new messages.
	//
	// It is safe to call Discard() multiple times.
	Discard()

	// IsDiscarded indicates whether the client has been "discarded"
	// and should no longer be used.
	IsDiscarded() bool

	// Send sends the specified message to the client's channel (if not discarded).
	Send(m Message)
}

Client is an interface for a generic subscription client.

type DefaultClient

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

DefaultClient defines a generic subscription client.

func NewDefaultClient

func NewDefaultClient() *DefaultClient

NewDefaultClient creates and returns a new DefaultClient instance.

func (*DefaultClient) Channel

func (c *DefaultClient) Channel() chan Message

Channel implements the Client.Channel interface method.

func (*DefaultClient) Discard

func (c *DefaultClient) Discard()

Discard implements the Client.Discard interface method.

func (*DefaultClient) Get

func (c *DefaultClient) Get(key string) any

Get implements the Client.Get interface method.

func (*DefaultClient) HasSubscription

func (c *DefaultClient) HasSubscription(sub string) bool

HasSubscription implements the Client.HasSubscription interface method.

func (*DefaultClient) Id

func (c *DefaultClient) Id() string

Id implements the Client.Id interface method.

func (*DefaultClient) IsDiscarded

func (c *DefaultClient) IsDiscarded() bool

IsDiscarded implements the Client.IsDiscarded interface method.

func (*DefaultClient) Send

func (c *DefaultClient) Send(m Message)

Send sends the specified message to the client's channel (if not discarded).

func (*DefaultClient) Set

func (c *DefaultClient) Set(key string, value any)

Set implements the Client.Set interface method.

func (*DefaultClient) Subscribe

func (c *DefaultClient) Subscribe(subs ...string)

Subscribe implements the Client.Subscribe interface method.

Empty subscriptions (aka. "") are ignored.

func (*DefaultClient) Subscriptions

func (c *DefaultClient) Subscriptions(prefixes ...string) map[string]SubscriptionOptions

Subscriptions implements the Client.Subscriptions interface method.

It returns a shallow copy of the the client subscriptions matching the prefixes. If no prefix is specified, returns all subscriptions.

func (*DefaultClient) Unset

func (c *DefaultClient) Unset(key string)

Unset implements the Client.Unset interface method.

func (*DefaultClient) Unsubscribe

func (c *DefaultClient) Unsubscribe(subs ...string)

Unsubscribe implements the Client.Unsubscribe interface method.

If subs is not set, this method removes all registered client's subscriptions.

type Message

type Message struct {
	Name string `json:"name"`
	Data []byte `json:"data"`
}

Message defines a client's channel data.

type SubscriptionOptions

type SubscriptionOptions struct {
	Query   map[string]any `json:"query"`
	Headers map[string]any `json:"headers"`
}

SubscriptionOptions defines the request options (query params, headers, etc.) for a single subscription topic.

Jump to

Keyboard shortcuts

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