sse-cluster: Index | Files

package broker

import ""

Package broker contains the broker implementation.


Package Files

broker.go channel.go client.go message.go

type Broker Uses

type Broker struct {
    // contains filtered or unexported fields

The Broker type represents a node in the cluster, it contains the list of all other members as well as a map of connected client channels.

func New Uses

func New(ml Memberlist, cl *http.Client) *Broker

New creates a new instance of the Broker type using the given member list and node.

func (*Broker) Close Uses

func (b *Broker) Close()

Close blocks the goroutine until all asynchronous operations of the broker have stopped.

func (*Broker) NewClient Uses

func (b *Broker) NewClient(channelID, clientID string) (*Client, error)

NewClient creates a new client for a given channel. If the channel does not exist, it is created.

func (*Broker) Publish Uses

func (b *Broker) Publish(channelID, clientID string, msg Message) error

Publish writes a given message to a client. If no client identifier is specified, the message is written to the entire channel. If running in a cluster, the event is forwarded asynchronously via HTTP to the next node whose id does not exist in the message's BeenTo field.

func (*Broker) RemoveClient Uses

func (b *Broker) RemoveClient(channelID, clientID string)

RemoveClient removes a client from a channel. If the channel has no connected clients, it is also removed.

func (*Broker) Status Uses

func (b *Broker) Status() *Status

Status returns information on the broker. It contains the number of running goroutines, the gossip members and total member count, as well as client information for this broker.

type Channel Uses

type Channel struct {
    // contains filtered or unexported fields

The Channel type represents a channel within the broker. Each channel has a unique identifier and can have one or more clients. When events are published to a channel, a client is chosen at random

func NewChannel Uses

func NewChannel(id string) *Channel

NewChannel creates a new instance of the Channel type using the given identifier

func (*Channel) ClientIDs Uses

func (c *Channel) ClientIDs() []string

ClientIDs returns an array of all client identifiers in this channel.

func (*Channel) NewClient Uses

func (c *Channel) NewClient(id string) (*Client, error)

NewClient adds a new client to the channel

func (*Channel) NumClients Uses

func (c *Channel) NumClients() int

NumClients returns the total number of clients for a channel.

func (*Channel) RemoveClient Uses

func (c *Channel) RemoveClient(id string)

RemoveClient removes a client from the channel

func (*Channel) Write Uses

func (c *Channel) Write(msg Message)

Write writes a given message to all clients in the channel

func (*Channel) WriteTo Uses

func (c *Channel) WriteTo(clientID string, msg Message)

WriteTo writes a message directly to a given client

type Client Uses

type Client struct {
    // contains filtered or unexported fields

The Client type represents a single client connected to the broker

func NewClient Uses

func NewClient(id string) *Client

NewClient creates a new instance of the Client type with the given identifier.

func (*Client) ID Uses

func (c *Client) ID() string

ID returns this client's identifier.

func (*Client) Messages Uses

func (c *Client) Messages() <-chan Message

Messages returns a read-only channel for this client's messages.

func (*Client) Write Uses

func (c *Client) Write(msg Message)

Write writes a given array of bytes to a client

type Memberlist Uses

type Memberlist interface {
    NumMembers() int
    LocalNode() *memberlist.Node
    Members() []*memberlist.Node

The Memberlist type represents the gossip implementation used by the broker for service discovery.

type Message Uses

type Message struct {
    // The event ID to set the EventSource object's last event ID value.
    ID  string `json:"id"`

    // A string identifying the type of event described. If this is specified, an event will
    // be dispatched on the browser to the listener for the specified event name;
    // the website source code should use addEventListener() to listen for named events.
    // The onmessage handler is called if no event name is specified for a message.
    Event string `json:"event"`

    // The data field for the message. When the EventSource receives multiple consecutive lines that begin with data:,
    // it will concatenate them, inserting a newline character between each one.
    // Trailing newlines are removed.
    Data json.RawMessage `json:"data"`

    // The reconnection time to use when attempting to send the event. This must be an integer,
    // specifying the reconnection time in milliseconds.
    // If a non-integer value is specified, the field is ignored.
    Retry int `json:"retry"`

    // Contains identifiers of previous nodes this event has been through
    BeenTo []string `json:"been_to"`

The Message type represents a server-sent event.

func (*Message) Bytes Uses

func (m *Message) Bytes() []byte

Bytes returns the Message instance in its textual form

func (*Message) JSON Uses

func (m *Message) JSON() []byte

JSON returns the Message instance in JSON encoding

type Status Uses

type Status struct {
    Goroutines int `json:"num_goroutines"`
    Gossip     struct {
        MemberCount int            `json:"member_count"`
        Members     map[string]int `json:"members"`
    }   `json:"gossip"`
    Channels map[string][]string `json:"channels"`

The Status type represents the status of a node/cluster. It contains sections for the gossip memberlist and the node's channels

Package broker imports 10 packages (graph) and is imported by 2 packages. Updated 2019-05-18. Refresh now. Tools for package owners.