Documentation ¶
Index ¶
Examples ¶
Constants ¶
View Source
const Scheme = "mqtt"
Scheme is the URL scheme mqttpubsub registers its URLOpeners under on pubsub.DefaultMux.
Variables ¶
This section is empty.
Functions ¶
func OpenSubscription ¶
func OpenSubscription(conn Subscriber, topicName string, opts *SubscriptionOptions) (*pubsub.Subscription, error)
Example ¶
package main import ( "context" "log" mqtt "github.com/eclipse/paho.mqtt.golang" mqttpubsub "github.com/frantjc/go-mqttpubsub" ) var ctx = context.Background() func main() { opts := mqtt.NewClientOptions() opts = opts.AddBroker("mqtt://mqtt.example.com") opts.ClientID = "exampleClient" cli := mqtt.NewClient(opts) defer cli.Disconnect(0) token := cli.Connect() if token.Wait() { if err := token.Error(); err != nil { log.Println(token.Error()) return } } subscription, err := mqttpubsub.OpenSubscription( mqttpubsub.NewSubscriber(cli, 0, 0), "example.mysubject", nil) if err != nil { log.Println(token.Error()) return } defer func() { _ = subscription.Shutdown(ctx) }() }
Output:
func OpenTopic ¶
Example ¶
package main import ( "context" "log" mqtt "github.com/eclipse/paho.mqtt.golang" mqttpubsub "github.com/frantjc/go-mqttpubsub" ) var ctx = context.Background() func main() { opts := mqtt.NewClientOptions() opts = opts.AddBroker("mqtt://mqtt.example.com") opts.ClientID = "exampleClient" cli := mqtt.NewClient(opts) defer cli.Disconnect(0) token := cli.Connect() if token.Wait() && token.Error() != nil { log.Println(token.Error()) return } topic, err := mqttpubsub.OpenTopic(mqttpubsub.NewPublisher(cli, 0, 0), "example.mysubject", nil) if err != nil { log.Println(err) return } defer func() { _ = topic.Shutdown(ctx) }() }
Output:
Types ¶
type Publisher ¶
type Subscriber ¶
type Subscriber interface { Subscribe(topic string, handler mqtt.MessageHandler, qos *byte) error Unsubscribe(topic string) error Close() error }
func NewSubscriber ¶
type SubscriptionOptions ¶
SubscriptionOptions sets options for constructing a *pubsub.Subscription backed by MQTT.
type TopicOptions ¶
type TopicOptions struct{}
TopicOptions sets options for constructing a *pubsub.Topic backed by MQTT.
type URLOpener ¶
type URLOpener struct { // Connection to use for communication with the server. Subscriber Publisher // TopicOptions specifies the options to pass to OpenTopic. TopicOptions TopicOptions // SubscriptionOptions specifies the options to pass to OpenSubscription. SubscriptionOptions SubscriptionOptions }
URLOpener opens MQTT URLs like "mqtt://myexchange" for topics or "mqtt://myqueue" for subscriptions.
For topics, the URL's host+path is used as the exchange name.
For subscriptions, the URL's host+path is used as the queue name.
No query parameters are supported.
func (*URLOpener) OpenSubscriptionURL ¶
func (o *URLOpener) OpenSubscriptionURL(_ context.Context, u *url.URL) (*pubsub.Subscription, error)
OpenSubscriptionURL opens a pubsub.Subscription based on u.
Click to show internal directories.
Click to hide internal directories.