Documentation ¶
Overview ¶
Copyright (c) 2021 Nutanix, Inc.
Package transport provides a Publish/Subscribe interface for publishing data from streams to data pipelines or subscribing to data from the data pipelines to publish to the streams.
transport package exposes two interfaces:
type Client interface { Publish(channel string, msg Message) error Subscribe(channel string, callback MessageHandler) (Subscription, error) }
and
type Subscription interface { Unsubscribe() error Channel() string }
A `Client` can be created by calling the `NewTransportClient` function:
client, err := NewTransportClient()
Note, the client created by the `NewTransportClient` function is a singleton. Repeated calls to the function will return the same client.
In order to publish data into the transport, we need to create a Message object:
msg := &Message{ Payload: []byte("example") }
Once, the client has been created, it can be used to Publish data from streams into the transport channel:
client.Publish(stream.GetTransportChannel(), msg)
In order to create a subscription, the client needs to provide a callback with the MessageHandler signature. This callback gets called with a message as parameter each time a new message is received on the subscribed channel.
func msgHandler (msg *Message) { // Do stuff }
Subsequently, a subscription can be created for subscribing to the data from the transport channel:
sub, err := client.Subscribe(stream.GetTransportChannel(), msgHandler)
A subscription also exposes the channel it is subscribed to via the `Channel` method:
channel := sub.Channel()
When a subscription is no longer needed, it can be unsubscribed by calling the `Unsubscribe` method:
sub.Unsubscribe()
Copyright (c) 2021 Nutanix, Inc.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface { // Publish publishes the message onto the provided channel Publish(channel string, msg Message) error // Subscribe subscribes all future messages on the channel and registers a callback Subscribe(channel string, callback MessageHandler) (Subscription, error) }
Client describes the publish / subscribe interface of the transport client
func NewTransportClient ¶
NewTransportClient returns a client for publishing and subscribing to datastreams from data pipelines
type Message ¶
type Message struct {
Payload []byte `json:"payload"`
}
Message defines the data structure of the messages conveyed by the transport
type MessageHandler ¶
type MessageHandler func(msg *Message)
MessageHandler defines the function signature for the callback function in a Subscribe call
type Subscription ¶
type Subscription interface { // Unsubscribe unsubscribes the connection Unsubscribe() error // Channel returns the channel the subscription belongs to Channel() string }
Subscription describes the interface of the subscription object