Kannaiyan
Kannaiyan

Reputation: 13055

How to create ConsumerGroups for Eventhub programatically using nodejs in Azure?

How to create consumergroups in eventhub using nodejs in Azure?

I tried to replicate what .net SDK offers, it did not work.

const { NamespaceManager } = require("@azure/service-bus");                                                                                                   
let namespaceManager = NamespaceManager.CreateFromConnectionString(eventHubConnectionString);                                                                 
let ehd = namespaceManager.GetEventHub(eventHubPath);                                                                                                         
namespaceManager.CreateConsumerGroupIfNotExists(ehd.Path, consumerGroupName);

Upvotes: 0

Views: 373

Answers (2)

Kannaiyan
Kannaiyan

Reputation: 13055

Here is the process that worked:

https://learn.microsoft.com/en-us/rest/api/eventhub/create-consumer-group

Steps:

  1. Create SAS Token
  2. Supply the right headers and make a https call to REST api
  3. You can create it only once, if you call for the second time, it will throw an 409 error. If you want an update or insert call, you need to check for it.

SAS Token:

https://learn.microsoft.com/en-us/rest/api/eventhub/generate-sas-token

uri -- url of your eventhub saName -- Name of your Managed Policy saKey -- Primary / Secondary Key of your EventHub Manage Policy (Ensure it has the Manage)

function createSharedAccessToken(uri, saName, saKey) { 
    if (!uri || !saName || !saKey) { 
            throw "Missing required parameter"; 
        } 
    var encoded = encodeURIComponent(uri); 
    var now = new Date(); 
    var week = 60*60*24*7;
    var ttl = Math.round(now.getTime() / 1000) + week;
    var signature = encoded + '\n' + ttl; 
    var signatureUTF8 = utf8.encode(signature); 
    var hash = crypto.createHmac('sha256', saKey).update(signatureUTF8).digest('base64'); 
    return 'SharedAccessSignature sr=' + encoded + '&sig=' +  
        encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName; 
}

Request Parameters:

URL:

https://your-namespace.servicebus.windows.net/your-event-hub/consumergroups/testCG?timeout=60&api-version=2014-01

Headers:

Content-Type: application/atom+xml;type=entry;charset=utf-8
Host: your-namespace.servicebus.windows.net
Authorization: {replace with the content from your SAS Token}

Payload:

<entry xmlns="http://www.w3.org/2005/Atom">  
   <content type="application/xml">  
      <ConsumerGroupDescription xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">Any name you want</ConsumerGroupDescription>  
   </content>  
</entry>

Possible Return Statuses:

201 -- Successful Creation
404 -- Not found, you are using a name that does not exist
409 -- The messaging entity 'XXX' already exists.

If you notice any other issues, please leave a comment.

Upvotes: 1

Serkant Karaca
Serkant Karaca

Reputation: 2042

Event hubs Node.JS SDK doesn't support management operations.

Try a management client like https://www.nuget.org/packages/Microsoft.Azure.Management.EventHub/

Upvotes: 0

Related Questions