title: How to connect to secured (Kerberos) Kafka cluster using a node.js and Kafka-node package
body: <p>I am working on a use case where I need to consume messages from Kafka (0.9.x) using a node.js (v6.9.5) and Kafka-node package. I am using below snippet to connect to Kafka from the node.js.</p>
  
  <p>My Kafka environment is Kerberos enabled. I don't know how to connect to the secured Kafka environment using a node.js. If anybody has done this before, please share the solution.</p>
  
  <pre><code>var payloads = [ { topic: 'nodejs_topic' }];
  var options = {
      groupId: 'kafka-node-group1',//consumer group id, default `kafka-node-group` 
      // Auto commit config 
      autoCommit: true,
      autoCommitIntervalMs: 5000,
      // The max wait time is the maximum amount of time in milliseconds to block waiting if insufficient data is available at the time the request is issued, default 100ms 
      fetchMaxWaitMs: 100,
      // This is the minimum number of bytes of messages that must be available to give a response, default 1 byte 
      fetchMinBytes: 1,
      // The maximum bytes to include in the message set for this partition. This helps bound the size of the response. 
      fetchMaxBytes: 1024 * 1024,
      // If set true, consumer will fetch message from the given offset in the payloads 
      fromOffset: false,
      //securityProtocol:'PLAINTEXTSASL'
      // If set to 'buffer', values will be returned as raw buffer objects. 
      encoding: 'utf8'
  };
  var kafka = require('kafka-node'),
      client = new kafka.Client('server-1:2181,server-2:2181,server-3:2181'),
      consumer = new kafka.Consumer(client,payloads,options);
  
  client.on('ready', function(){
    console.log('Client ready!');
  });
  
  //console.log(client);
  //console.log(consumer);
  
  consumer.on('error', function (err) {
    console.log("Kafka Error: Consumer - " + err);
  });
  
  consumer.on('offsetOutOfRange', function (err){
    console.log("Kafka offsetOutOfRange: " + err);
  });
  
  consumer.on('message', function(message){
    console.log(message);
  });
  </code></pre>