subscriptions
import subscriptions "ganigeorgiev.com/pocketbase/pocketbase/tools/subscriptions"
Source: tools/subscriptions.
Functions
SendOrdered
func SendOrdered(client Client, m Message)
SendOrdered queues m for the client and returns without waiting for the client to read it.
The queued messages of a client are delivered with client.Send one at a time, in the order they were queued, by a single goroutine that exits once the queue is empty. Use it instead of `go client.Send(m)`, which delivers concurrent sends in any order.
Types
Broker
type Broker struct {
// contains filtered or unexported fields
}
Broker defines a struct for managing subscriptions clients.
NewBroker
func NewBroker() *Broker
NewBroker initializes and returns a new Broker instance.
Broker.ChunkedClients
func (b *Broker) ChunkedClients(chunkSize int) [][]Client
ChunkedClients splits the current clients into a chunked slice.
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.
Broker.Clients
func (b *Broker) Clients() map[string]Client
Clients returns a shallow copy of all registered clients indexed with their connection id.
Broker.Register
func (b *Broker) Register(client Client)
Register adds a new client to the broker instance.
Broker.TotalClients
func (b *Broker) TotalClients() int
TotalClients returns the total number of registered clients.
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.
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.
DefaultClient
type DefaultClient struct {
// contains filtered or unexported fields
}
DefaultClient defines a generic subscription client.
NewDefaultClient
func NewDefaultClient() *DefaultClient
NewDefaultClient creates and returns a new DefaultClient instance.
DefaultClient.Channel
func (c *DefaultClient) Channel() chan Message
Channel implements the Client.Channel interface method.
DefaultClient.Discard
func (c *DefaultClient) Discard()
Discard implements the Client.Discard interface method.
Pending sends return without delivering their message and the channel is closed once they have.
DefaultClient.Get
func (c *DefaultClient) Get(key string) any
Get implements the Client.Get interface method.
DefaultClient.HasSubscription
func (c *DefaultClient) HasSubscription(sub string) bool
HasSubscription implements the Client.HasSubscription interface method.
DefaultClient.Id
func (c *DefaultClient) Id() string
Id implements the Client.Id interface method.
DefaultClient.IsDiscarded
func (c *DefaultClient) IsDiscarded() bool
IsDiscarded implements the Client.IsDiscarded interface method.
DefaultClient.Send
func (c *DefaultClient) Send(m Message)
Send sends the specified message to the client's channel (if not discarded).
It waits until the message is read or the client is discarded.
DefaultClient.Set
func (c *DefaultClient) Set(key string, value any)
Set implements the Client.Set interface method.
DefaultClient.Subscribe
func (c *DefaultClient) Subscribe(subs ...string)
Subscribe implements the Client.Subscribe interface method.
Empty subscriptions (aka. "") are ignored.
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.
DefaultClient.Unset
func (c *DefaultClient) Unset(key string)
Unset implements the Client.Unset interface method.
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.
Message
type Message struct {
Name string `json:"name"`
Data []byte `json:"data"`
}
Message defines a client's channel data.
Message.WriteSSE
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()
SubscriptionOptions
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.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.