github.com/Jeffail/benthos/v3@v3.65.0/lib/input/mqtt.go (about)

     1  package input
     2  
     3  import (
     4  	"github.com/Jeffail/benthos/v3/internal/docs"
     5  	"github.com/Jeffail/benthos/v3/internal/mqttconf"
     6  	"github.com/Jeffail/benthos/v3/lib/input/reader"
     7  	"github.com/Jeffail/benthos/v3/lib/log"
     8  	"github.com/Jeffail/benthos/v3/lib/metrics"
     9  	"github.com/Jeffail/benthos/v3/lib/types"
    10  	"github.com/Jeffail/benthos/v3/lib/util/tls"
    11  )
    12  
    13  //------------------------------------------------------------------------------
    14  
    15  func init() {
    16  	Constructors[TypeMQTT] = TypeSpec{
    17  		constructor: fromSimpleConstructor(NewMQTT),
    18  		Summary: `
    19  Subscribe to topics on MQTT brokers.`,
    20  		Description: `
    21  ### Metadata
    22  
    23  This input adds the following metadata fields to each message:
    24  
    25  ` + "``` text" + `
    26  - mqtt_duplicate
    27  - mqtt_qos
    28  - mqtt_retained
    29  - mqtt_topic
    30  - mqtt_message_id
    31  ` + "```" + `
    32  
    33  You can access these metadata fields using
    34  [function interpolation](/docs/configuration/interpolation#metadata).`,
    35  		FieldSpecs: docs.FieldSpecs{
    36  			docs.FieldCommon("urls", "A list of URLs to connect to. If an item of the list contains commas it will be expanded into multiple URLs.").Array(),
    37  			docs.FieldCommon("topics", "A list of topics to consume from.").Array(),
    38  			docs.FieldCommon("client_id", "An identifier for the client connection."),
    39  			docs.FieldString("dynamic_client_id_suffix", "Append a dynamically generated suffix to the specified `client_id` on each run of the pipeline. This can be useful when clustering Benthos producers.").Optional().Advanced().HasAnnotatedOptions(
    40  				"nanoid", "append a nanoid of length 21 characters",
    41  			),
    42  			docs.FieldAdvanced("qos", "The level of delivery guarantee to enforce.").HasOptions("0", "1", "2"),
    43  			docs.FieldAdvanced("clean_session", "Set whether the connection is non-persistent."),
    44  			mqttconf.WillFieldSpec(),
    45  			docs.FieldString("connect_timeout", "The maximum amount of time to wait in order to establish a connection before the attempt is abandoned.", "1s", "500ms").HasDefault("30s").AtVersion("3.58.0"),
    46  			docs.FieldAdvanced("user", "A username to assume for the connection."),
    47  			docs.FieldAdvanced("password", "A password to provide for the connection."),
    48  			docs.FieldAdvanced("keepalive", "Max seconds of inactivity before a keepalive message is sent."),
    49  			tls.FieldSpec().AtVersion("3.45.0"),
    50  			docs.FieldDeprecated("stale_connection_timeout"),
    51  		},
    52  		Categories: []Category{
    53  			CategoryServices,
    54  		},
    55  	}
    56  }
    57  
    58  //------------------------------------------------------------------------------
    59  
    60  // NewMQTT creates a new MQTT input type.
    61  func NewMQTT(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
    62  	m, err := reader.NewMQTT(conf.MQTT, log, stats)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return NewAsyncReader(
    67  		TypeMQTT,
    68  		true,
    69  		reader.NewAsyncPreserver(m),
    70  		log, stats,
    71  	)
    72  }
    73  
    74  //------------------------------------------------------------------------------