SDK Reference

This section provides a detailed reference for the Azure Event Hubs SDK, covering its main classes, methods, and properties for various programming languages.

JavaScript SDK (v5.x)

EventHubProducerClient

Client for sending events to an Event Hub.

Constructor

Creates an instance of EventHubProducerClient.

new EventHubProducerClient(connectionString: string, eventHubName: string, options?: ProducerClientOptions)

Parameters

connectionString (string)
The connection string for your Event Hubs namespace.
eventHubName (string)
The name of the Event Hub to connect to.
options (ProducerClientOptions, optional)
Optional configuration settings for the producer client.

sendBatch

Sends a batch of events to the Event Hub.

async sendBatch(events: EventData[] | SendEventsOptions, options?: SendEventsOptions): Promise<SendEventsResult>

Parameters

events (EventData[] | SendEventsOptions)
An array of EventData objects or an object with an events array and optional partition key.
options (SendEventsOptions, optional)
Optional settings for sending the batch, such as partition key.

Returns

Promise<SendEventsResult>
A promise that resolves with the result of the send operation.

close

Closes the producer client and releases any resources.

async close(): Promise<void>

EventHubConsumerClient

Client for receiving events from an Event Hub.

Constructor

Creates an instance of EventHubConsumerClient.

new EventHubConsumerClient(consumerGroup: string, connectionString: string, eventHubName: string, options?: ConsumerClientOptions)

Parameters

consumerGroup (string)
The name of the consumer group to join.
connectionString (string)
The connection string for your Event Hubs namespace.
eventHubName (string)
The name of the Event Hub to connect to.
options (ConsumerClientOptions, optional)
Optional configuration settings for the consumer client.

subscribe

Subscribes to receive events from the Event Hub.

subscribe(handlers: ConsumeEventHandler, options?: SubscribeOptions): Subscription

Parameters

handlers (ConsumeEventHandler)
An object containing event handler functions (processEvents, processError).
options (SubscribeOptions, optional)
Optional settings for subscription, such as initial partition offset.

Returns

Subscription
A subscription object that can be used to manage the subscription.

The ConsumeEventHandler interface:

interface ConsumeEventHandler {
    processEvents(events: EventData[], context: SubscribeContext): Promise<void>;
    processError(error: Error, context: SubscribeContext): Promise<void>;
}

close

Closes the consumer client and releases any resources.

async close(): Promise<void>

Note

This reference covers the core JavaScript SDK (v5.x). For other languages like Python, Java, .NET, and Go, please refer to the respective documentation linked in the sidebar.

Common Data Types

EventData

Represents a single event message.

{
    body: any; // The event body. Can be string, buffer, or other serializable types.
    partitionKey?: string; // Optional partition key.
    properties?: {[key: string]: any}; // Optional custom properties.
    systemProperties?: {[key: string]: any}; // Optional system properties.
    offset: number; // The offset of the event within the partition.
    sequenceNumber: number; // The sequence number of the event within the partition.
    enqueuedTime: Date; // The UTC enqueued time of the event.
    toString(): string; // String representation of the event.
}

SendEventsResult

Result of a batch send operation.

{
    receivedSequenceNumbers: number[]; // Sequence numbers of the successfully sent events.
    lastSequenceNumber?: number; // The sequence number of the last event in the batch.
    lastEnqueuedTime?: Date; // The enqueued time of the last event in the batch.
}

Tip

Always ensure you handle potential errors gracefully, especially during event transmission and reception. Refer to the 'Error Handling' section for best practices.