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

subscriptions

package
v0.40.4 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 11 Imported by: 69

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) ChunkedClients added in v0.23.0

func (b *Broker) ChunkedClients(chunkSize int) [][]Client

ChunkedClients splits the current clients into a chunked slice.

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) TotalClients added in v0.23.0

func (b *Broker) TotalClients() int

TotalClients returns the total number of registered clients.

func (*Broker) Unregister

func (b *Broker) Unregister(clientId string)

Unregister removes a single client by its id and marks it as discarded.

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.
	//
	// NB! The channel shouldn't be used after calling Discard().
	Channel() chan Message

	// Subscriptions returns a shallow copy of 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" (and closes its channel),
	// 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 added in v0.11.3

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 added in v0.11.3

func (c *DefaultClient) IsDiscarded() bool

IsDiscarded implements the Client.IsDiscarded interface method.

func (*DefaultClient) Send added in v0.17.0

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 client subscriptions matching the prefixes. If no prefix is specified, returns all subscriptions.

func (*DefaultClient) Unset added in v0.17.0

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.

func (*Message) WriteSSE added in v0.26.0

func (m *Message) WriteSSE(w io.Writer, eventId string) error

WriteSSE writes the current message in a SSE format into the provided writer.

For example, writing to a router.Event:

m := Message{Name: "users/create", Data: []byte{...}}
m.WriteSSE(e.Response, "yourEventId")
e.Flush()

type SubscriptionOptions added in v0.20.0

type SubscriptionOptions struct {
	Query   map[string]string `json:"query"`
	Headers map[string]string `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