Azure Functions Event Hubs bindings

This article explains how to configure and use Azure Event Hubs bindings for Azure Functions. Azure Functions supports trigger and output bindings for Event Hubs.

This is reference information for Azure Functions developers. If you're new to Azure Functions, start with the following resources:

If you are new to Azure Event Hubs, see the Event Hubs overview.

Event hub trigger

Use the Event Hubs trigger to respond to an event sent to an event hub event stream. You must have read access to the event hub to set up the trigger.

The Event Hubs function trigger uses the following JSON object in the bindings array of function.json:

{
    "type": "eventHubTrigger",
    "name": "<Name of trigger parameter in function signature>",
    "direction": "in",
    "path": "<Name of the event hub>",
    "consumerGroup": "Consumer group to use - see below",
    "connection": "<Name of app setting with connection string - see below>"
}

consumerGroup is an optional property used to set the consumer group used to subscribe to events in the hub. If omitted, the $Default consumer group is used.
connection must be the name of an app setting that contains the connection string to the event hub's namespace. Copy this connection string by clicking the Connection Information button for the namespace, not the event hub itself. This connection string must have at least read permissions to activate the trigger.

Additional settings can be provided in a host.json file to further fine tune Event Hubs triggers.

Trigger usage

When an Event Hubs trigger function is triggered, the message that triggers it is passed into the function as a string.

Trigger sample

Suppose you have the following Event Hubs trigger in the bindings array of function.json:

{
  "type": "eventHubTrigger",
  "name": "myEventHubMessage",
  "direction": "in",
  "path": "MyEventHub",
  "connection": "myEventHubReadConnectionString"
}

See the language-specific sample that logs the message body of the event hub trigger.

Trigger sample in C#

using System;

public static void Run(string myEventHubMessage, TraceWriter log)
{
    log.Info($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
}

Trigger sample in F#

let Run(myEventHubMessage: string, log: TraceWriter) =
    log.Info(sprintf "F# eventhub trigger function processed work item: %s" myEventHubMessage)

Trigger sample in Node.js

module.exports = function (context, myEventHubMessage) {
    context.log('Node.js eventhub trigger function processed work item', myEventHubMessage);    
    context.done();
};

Event Hubs output binding

Use the Event Hubs output binding to write events to an event hub event stream. You must have send permission to an event hub to write events to it.

The output binding uses the following JSON object in the bindings array of function.json:

{
    "type": "eventHub",
    "name": "<Name of output parameter in function signature>",
    "path": "<Name of event hub>",
    "connection": "<Name of app setting with connection string - see below>"
    "direction": "out"
}

connection must be the name of an app setting that contains the connection string to the event hub's namespace. Copy this connection string by clicking the Connection Information button for the namespace, not the event hub itself. This connection string must have send permissions to send the message to the event stream.

Output usage

This section shows you how to use your Event Hubs output binding in your function code.

You can output messages to the configured event hub with the following parameter types:

  • out string
  • ICollector<string> (to output multiple messages)
  • IAsyncCollector<string> (async version of ICollector<T>)

Output sample

Suppose you have the following Event Hubs output binding in the bindings array of function.json:

{
    "type": "eventHub",
    "name": "outputEventHubMessage",
    "path": "myeventhub",
    "connection": "MyEventHubSend",
    "direction": "out"
}

See the language-specific sample that writes an event to the even stream.

Output sample in C#

using System;

public static void Run(TimerInfo myTimer, out string outputEventHubMessage, TraceWriter log)
{
    String msg = $"TimerTriggerCSharp1 executed at: {DateTime.Now}";
    log.Verbose(msg);   
    outputEventHubMessage = msg;
}

Or, to create multiple messages:

public static void Run(TimerInfo myTimer, ICollector<string> outputEventHubMessage, TraceWriter log)
{
    string message = $"Event Hub message created at: {DateTime.Now}";
    log.Info(message);
    outputEventHubMessage.Add("1 " + message);
    outputEventHubMessage.Add("2 " + message);
}

Output sample in F#

let Run(myTimer: TimerInfo, outputEventHubMessage: byref<string>, log: TraceWriter) =
    let msg = sprintf "TimerTriggerFSharp1 executed at: %s" DateTime.Now.ToString()
    log.Verbose(msg);
    outputEventHubMessage <- msg;

Output sample for Node.js

module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();
    context.log('Event Hub message created at: ', timeStamp);   
    context.bindings.outputEventHubMessage = "Event Hub message created at: " + timeStamp;
    context.done();
};

Or, to send multiple messages,

module.exports = function(context) {
    var timeStamp = new Date().toISOString();
    var message = 'Event Hub message created at: ' + timeStamp;

    context.bindings.outputEventHubMessage = [];

    context.bindings.outputEventHubMessage.push("1 " + message);
    context.bindings.outputEventHubMessage.push("2 " + message);
    context.done();
};

Next steps

For information about other bindings and triggers for Azure Functions, see Azure Functions triggers and bindings developer reference.