Closed remster closed 3 years ago
Good point. mqtt.Publish is the MQTT control packet data. I modified the client APIs and now it looks as below. The parameters are more explicit in the API.
type Client interface {
// Connect connect with MQTT broker and send CONNECT MQTT request
Connect(ctx context.Context) (*ConnAck, error)
// Disconnect disconnect from MQTT broker
Disconnect(ctx context.Context, reasonCode DisconnectReasonCode, props *DisconnectProperties) error
// Subscribe send MQTT Subscribe request to the broker with the given Subscription parameter and it's
// properties, and a message channel through which the published messages are returned for the given
// subscribed topics.
// The function waits for the the MQTT SUBSCRIBE response, SubAck, or a packet timeout
// configured as part of the Client options
// Note: the subscriptions input may contain more than one topic, the associated
// MessageReceiver is valid for all the topics present in the given subscriptions.
Subscribe(ctx context.Context, subscriptions []*Subscription, props *SubscribeProperties, recvr *MessageReceiver) (*SubAck, error)
// CallbackSubscribe send MQTT Subscribe request to the broker with the given Subscription parameter and it's
// properties, and a callback handler through which the published messages are returned for the given subscribed topics.
// The function waits for the the MQTT SUBSCRIBE response, SubAck, or a packet timeout
// configured as part of the Client options
// Note: the input Subscribe parameter can contain more than one topic, the associated
// Callback handler is valid for all the topics present in the given Subscribe.
CallbackSubscribe(ctx context.Context, subscriptions []*Subscription, props *SubscribeProperties, cb MessageHandler) (*SubAck, error)
// Unsubscribe send MQTT UNSUBSCRIBE request to the broker with the given topic filters and it's properties.
// The function waits for the the MQTT SUBSCRIBE response, SubAck, or for a packet timeout
Unsubscribe(ctx context.Context, topicFilters []string, props *UnsubscribeProperties) (*UnsubAck, error)
// Publish send MQTT PUBLISH packet to the MQTT broker. When the QoS is 1 or 2
// the function waits for a response from the broker and for QoS 0 the function
// complets immediately after the PUBLISH message is scheduled to send.
Publish(ctx context.Context, topic string, qosLevel byte, retain bool, payload []byte, props *PublishProperties) error
// On map the callback argument with the event name, more than one
// callback can be added for a particular event name
On(eventName string, callback interface{}) error
// Off removed the callback associated with the event name
Off(eventName string, value interface{}) error
}
merged
https://github.com/srishina/mqtt.go/blob/main/examples/client-pub/main.go#L109
err = client.Publish(context.Background(), &mqtt.Publish{TopicName: topic, QoSLevel: byte(qosLevel), Payload: []byte(payload)})
the separation of concerns betweenclient.Publish
andmqtt.Publish
is not clear. i think i would drop the latter, just:err = client.Publish(context.Background(), topic, qosLevel, []byte(payload)}