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

     1  package input
     2  
     3  import (
     4  	"github.com/Jeffail/benthos/v3/internal/docs"
     5  	"github.com/Jeffail/benthos/v3/lib/input/reader"
     6  	"github.com/Jeffail/benthos/v3/lib/log"
     7  	"github.com/Jeffail/benthos/v3/lib/metrics"
     8  	"github.com/Jeffail/benthos/v3/lib/types"
     9  	"github.com/Jeffail/benthos/v3/lib/util/http/auth"
    10  	btls "github.com/Jeffail/benthos/v3/lib/util/tls"
    11  )
    12  
    13  //------------------------------------------------------------------------------
    14  
    15  func init() {
    16  	Constructors[TypeWebsocket] = TypeSpec{
    17  		constructor: fromSimpleConstructor(NewWebsocket),
    18  		Summary: `
    19  Connects to a websocket server and continuously receives messages.`,
    20  		Description: `
    21  It is possible to configure an ` + "`open_message`" + `, which when set to a
    22  non-empty string will be sent to the websocket server each time a connection is
    23  first established.`,
    24  		FieldSpecs: append(docs.FieldSpecs{
    25  			docs.FieldCommon("url", "The URL to connect to.", "ws://localhost:4195/get/ws").HasType("string"),
    26  			docs.FieldAdvanced("open_message", "An optional message to send to the server upon connection."),
    27  			btls.FieldSpec(),
    28  		}, auth.FieldSpecs()...),
    29  		Categories: []Category{
    30  			CategoryNetwork,
    31  		},
    32  	}
    33  }
    34  
    35  //------------------------------------------------------------------------------
    36  
    37  // NewWebsocket creates a new Websocket input type.
    38  func NewWebsocket(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
    39  	ws, err := reader.NewWebsocket(conf.Websocket, log, stats)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return NewAsyncReader("websocket", true, reader.NewAsyncPreserver(ws), log, stats)
    44  }
    45  
    46  //------------------------------------------------------------------------------