bayeux

package module
v0.0.0-...-620eda6 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: MIT Imports: 11 Imported by: 1

README

go-bayeux-client

This package implements a client conditionally compliant with the Bayeux Protocol. It uses long-polling, not websockets. Messages are delivered on a channel provided by the user of the library, and can thus be buffered or unbuffered.

Example:

import "github.com/andreas/go-bayeux-client"

func main() {
	// The second argument is a *http.Client, which defaults to http.DefaultClient.
	// This can be used to control timeouts, etc.
	client := bayeux.NewClient("https://foo.com", nil)

	// Messages will be delivered on this channel (use buffered chan if needed).
	msgs := make(bayeux.Chan)

	// Subscribe will automatically handshake with the server if not connected.
	if err := client.Subscribe("/bar/baz", msgs); err != nil {
		panic(err)
	}
	// Disconnect from server and stop background loop.
	defer client.Close()

	for msg := range msgs {
		fmt.Println("Received message: ", msg)
	}
}

Limitations

  • Does not support publishing.
  • Only supports long-polling transport/connection type.
  • Limited advice support.

Documentation

Index

Constants

View Source
const (
	VERSION         = "1.0"
	MINIMUM_VERSION = "1.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client allows connecting to a Bayeux server and subscribing to channels.

func NewClient

func NewClient(url string, httpClient *http.Client) *Client

NewClient initialises a new Bayeux client. By default `http.DefaultClient` is used for HTTP connections.

func (*Client) Close

func (c *Client) Close() error

Close notifies the Bayeux server of the intent to disconnect and terminates the background polling loop.

func (*Client) Connect

func (c *Client) Connect() error

Connect performs a handshake with the server and will repeatedly initiate a long-polling connection until `Close` is called on the client.

func (*Client) Subscribe

func (c *Client) Subscribe(pattern string, out chan<- *Message) error

Subscribe is like `SubscribeExt` with a blank `ext` part.

func (*Client) SubscribeExt

func (c *Client) SubscribeExt(pattern string, out chan<- *Message, ext interface{}) error

SubscribeExt creates a new subscription on the Bayeux server. Messages for the subscription will be delivered on the given channel `out`. If the client has not performed a handshake already, it will do so first.

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(pattern string) error

type Message

type Message struct {
	Channel   string          `json:"channel"`
	Data      json.RawMessage `json:"data,omitempty"`
	Id        string          `json:"id,omitempty"`
	ClientId  string          `json:"clientId,omitempty"`
	Extension interface{}     `json:"ext,omitempty"`
}

Message is the type delivered to subscribers.

Jump to

Keyboard shortcuts

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