Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EventHubProducerClient

Package version

The EventHubProducerClient class is used to send events to an Event Hub.

There are multiple ways to create an EventHubProducerClient

  • Use the connection string from the SAS policy created for your Event Hub instance.
  • Use the connection string from the SAS policy created for your Event Hub namespace, and the name of the Event Hub instance
  • Use the full namespace like <yournamespace>.servicebus.windows.net, and a credentials object.

Optionally, you can also pass an options bag to configure the retry policy or proxy settings.

Hierarchy

  • EventHubProducerClient

Index

Constructors

constructor

  • The EventHubProducerClient class is used to send events to an Event Hub. Use the options parmeter to configure retry policy or proxy settings.

    Parameters

    • connectionString: string

      The connection string to use for connecting to the Event Hub instance. It is expected that the shared key properties and the Event Hub path are contained in this connection string. e.g. 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;EntityPath=my-event-hub-name'.

    • Optional options: EventHubClientOptions

      A set of options to apply when configuring the client.

      • retryOptions : Configures the retry policy for all the operations on the client. For example, { "maxRetries": 4 } or { "maxRetries": 4, "retryDelayInMs": 30000 }.
      • webSocketOptions: Configures the channelling of the AMQP connection over Web Sockets.
      • userAgent : A string to append to the built in user agent string that is passed to the service.

    Returns EventHubProducerClient

  • The EventHubProducerClient class is used to send events to an Event Hub. Use the options parmeter to configure retry policy or proxy settings.

    Parameters

    • connectionString: string

      The connection string to use for connecting to the Event Hub instance. It is expected that the shared key properties and the Event Hub path are contained in this connection string. e.g. 'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key;EntityPath=my-event-hub-name'.

    • eventHubName: string

      The name of the specific Event Hub to connect the client to.

    • Optional options: EventHubClientOptions

      A set of options to apply when configuring the client.

      • retryOptions : Configures the retry policy for all the operations on the client. For example, { "maxRetries": 4 } or { "maxRetries": 4, "retryDelayInMs": 30000 }.
      • webSocketOptions: Configures the channelling of the AMQP connection over Web Sockets.
      • userAgent : A string to append to the built in user agent string that is passed to the service.

    Returns EventHubProducerClient

  • The EventHubProducerClient class is used to send events to an Event Hub. Use the options parmeter to configure retry policy or proxy settings.

    Parameters

    • fullyQualifiedNamespace: string

      The full namespace which is likely to be similar to .servicebus.windows.net

    • eventHubName: string

      The name of the specific Event Hub to connect the client to.

    • credential: TokenCredential

      An credential object used by the client to get the token to authenticate the connection with the Azure Event Hubs service. See @azure/identity for creating the credentials.

    • Optional options: EventHubClientOptions

      A set of options to apply when configuring the client.

      • retryOptions : Configures the retry policy for all the operations on the client. For example, { "maxRetries": 4 } or { "maxRetries": 4, "retryDelayInMs": 30000 }.
      • webSocketOptions: Configures the channelling of the AMQP connection over Web Sockets.
      • userAgent : A string to append to the built in user agent string that is passed to the service.

    Returns EventHubProducerClient

Accessors

eventHubName

  • get eventHubName(): string

fullyQualifiedNamespace

  • get fullyQualifiedNamespace(): string
  • readonly

    The fully qualified namespace of the Event Hub instance for which this client is created. This is likely to be similar to .servicebus.windows.net.

    Returns string

Methods

close

  • close(): Promise<void>
  • Closes the AMQP connection to the Event Hub instance, returning a promise that will be resolved when disconnection is completed.

    throws

    Error if the underlying connection encounters an error while closing.

    Returns Promise<void>

    Promise

createBatch

  • Creates an instance of EventDataBatch to which one can add events until the maximum supported size is reached. The batch can be passed to the sendBatch method of the EventHubProducerClient to be sent to Azure Event Hubs.

    Example usage:

    const client = new EventHubProducerClient(connectionString);
    let batch = await client.createBatch();
    for (let i = 0; i < messages.length; i++) {
     if (!batch.tryAdd(messages[i])) {
       await client.sendBatch(batch);
       batch = await client.createBatch();
       if (!batch.tryAdd(messages[i])) {
         throw new Error("Message too big to fit")
       }
       if (i === messages.length - 1) {
         await client.sendBatch(batch);
       }
      }
    }
    throws

    Error if both partitionId and partitionKey are set in the options.

    throws

    Error if the underlying connection has been closed, create a new EventHubProducerClient.

    throws

    AbortError if the operation is cancelled via the abortSignal in the options.

    Parameters

    • Default value options: CreateBatchOptions = {}

      Configures the behavior of the batch.

      • partitionKey : A value that is hashed and used by the Azure Event Hubs service to determine the partition to which the events need to be sent.
      • partitionId : Id of the partition to which the batch of events need to be sent.
      • maxSizeInBytes: The upper limit for the size of batch. The tryAdd function will return false after this limit is reached.
      • abortSignal : A signal the request to cancel the operation.

    Returns Promise<EventDataBatch>

    Promise

getEventHubProperties

  • Provides the Event Hub runtime information.

    throws

    Error if the underlying connection has been closed, create a new EventHubProducerClient.

    throws

    AbortError if the operation is cancelled via the abortSignal.

    Parameters

    Returns Promise<EventHubProperties>

    A promise that resolves with information about the Event Hub instance.

getPartitionIds

  • Provides the id for each partition associated with the Event Hub.

    throws

    Error if the underlying connection has been closed, create a new EventHubProducerClient.

    throws

    AbortError if the operation is cancelled via the abortSignal.

    Parameters

    Returns Promise<Array<string>>

    A promise that resolves with an Array of strings representing the id for each partition associated with the Event Hub.

getPartitionProperties

  • Provides information about the state of the specified partition.

    throws

    Error if the underlying connection has been closed, create a new EventHubProducerClient.

    throws

    AbortError if the operation is cancelled via the abortSignal.

    Parameters

    • partitionId: string

      The id of the partition for which information is required.

    • Default value options: GetPartitionPropertiesOptions = {}

      The set of options to apply to the operation call.

    Returns Promise<PartitionProperties>

    A promise that resolves with information about the state of the partition .

sendBatch

  • Sends an array of events to the associated Event Hub.

    Example usage:

    const client = new EventHubProducerClient(connectionString);
    await client.sendBatch(messages);
    throws

    AbortError if the operation is cancelled via the abortSignal.

    throws

    MessagingError if an error is encountered while sending a message.

    throws

    Error if the underlying connection or sender has been closed.

    Parameters

    • batch: EventData[]

      An array of EventData.

    • Optional options: SendBatchOptions

      A set of options that can be specified to influence the way in which events are sent to the associated Event Hub.

      • abortSignal : A signal the request to cancel the send operation.
      • partitionId : The partition this batch will be sent to. If set, partitionKey can not be set.
      • partitionKey : A value that is hashed to produce a partition assignment. If set, partitionId can not be set.

    Returns Promise<void>

    Promise

  • Sends a batch of events to the associated Event Hub.

    Example usage:

    const client = new EventHubProducerClient(connectionString);
    let batch = await client.createBatch();
    for (let i = 0; i < messages.length; i++) {
     if (!batch.tryAdd(messages[i])) {
       await client.sendBatch(batch);
       batch = await client.createBatch();
       if (!batch.tryAdd(messages[i])) {
         throw new Error("Message too big to fit")
       }
       if (i === messages.length - 1) {
         await client.sendBatch(batch);
       }
      }
    }
    throws

    AbortError if the operation is cancelled via the abortSignal.

    throws

    MessagingError if an error is encountered while sending a message.

    throws

    Error if the underlying connection or sender has been closed.

    Parameters

    • batch: EventDataBatch

      A batch of events that you can create using the createBatch method.

    • Optional options: OperationOptions

      A set of options that can be specified to influence the way in which events are sent to the associated Event Hub.

      • abortSignal : A signal the request to cancel the send operation.

    Returns Promise<void>

    Promise

Generated using TypeDoc