The sample scripts in this article demonstrate how to connect to your Aiven for Apache Kafka service and pass a few messages.
Requirements:
node-rdkafka
Note: Install this library with SSL support enabled.
To test your Aiven for Apache Kafka service:
Download the SSL certificate files in the Aiven web console.
Go to the Overview page of your Aiven for Apache Kafka service.
Click Download next to Access Key and save the
service.key
file.Click Download next to Access Certificate and save the
service.cert
file.Click Download next to CA Certificate and save the
ca.pem
file.
Go to the Topics page for your service and add a new topic named
demo-topic
.Create and run the producer script:
var Kafka = require('node-rdkafka');
console.log(Kafka.features); // this should print 'ssl', among other things
var producer = new Kafka.Producer({
'metadata.broker.list': 'getting-started-with-kafka.htn-aiven-demo.aivencloud.com:17705',
'security.protocol': 'ssl',
'ssl.key.location': 'service.key',
'ssl.certificate.location': 'service.cert',
'ssl.ca.location': 'ca.pem',
'dr_cb': true
});
producer.connect();
producer.on('ready', function() {
try {
producer.produce(
'demo-topic', // topic to send the message to
null, // partition, null for librdkafka default partitioner
Buffer.from('Hello world!'), // value
null, // optional key
Date.now() // optional timestamp
);
producer.flush(2000);
console.log('Message sent successfully');
} catch (err) {
console.log('Failed to send message', err);
}
producer.disconnect();
});Create the consumer script:
var Kafka = require('node-rdkafka');
var stream = new Kafka.createReadStream({
'metadata.broker.list': 'getting-started-with-kafka.htn-aiven-demo.aivencloud.com:17705',
'group.id': 'demo-consumer-group',
'security.protocol': 'ssl',
'ssl.key.location': 'service.key',
'ssl.certificate.location': 'service.cert',
'ssl.ca.location': 'ca.pem'
}, {}, {'topics': ['demo-topic']});
stream.on('data', function(message) {
console.log('Got message');
console.log(message.value.toString());
});
If your Kafka is configured to use SASL authentication:
Copy your password from the Aiven web console.
Go to the Topics page for your service and add a new topic named
demo-topic
.Create and run the producer script:
var Kafka = require('node-rdkafka');
console.log(Kafka.features); // this should print 'sasl_ssl', among other things
var producer = new Kafka.Producer({
'metadata.broker.list': 'getting-started-with-kafka.htn-aiven-demo.aivencloud.com:24960',
'security.protocol': 'sasl_ssl',
'sasl.mechanism': 'SCRAM-SHA-512', // supports: PLAIN, SCRAM-SHA-256, SCRAM-SHA-512
'sasl.username': 'avnadmin',
'sasl.password': 'your_password_from_step1',
'ssl.ca.location': 'ca.pem',
'dr_cb': true
});
producer.connect();
producer.on('ready', function() {
try {
producer.produce(
'demo-topic', // topic to send the message to
null, // partition, null for librdkafka default partitioner
Buffer.from('Hello world!'), // value
null, // optional key
Date.now() // optional timestamp
);
producer.flush(2000);
console.log('Message sent successfully');
} catch (err) {
console.log('Failed to send message', err);
}
producer.disconnect();
});Create the consumer script:
var Kafka = require('node-rdkafka');
var stream = new Kafka.createReadStream({
'metadata.broker.list': 'getting-started-with-kafka.htn-aiven-demo.aivencloud.com:24960',
'group.id': 'demo-consumer-group',
'security.protocol': 'sasl_ssl',
'sasl.mechanism': 'SCRAM-SHA-512', // supports: PLAIN, SCRAM-SHA-256, SCRAM-SHA-512
'sasl.username': 'avnadmin',
'sasl.password': 'your_password_from_step1',
'ssl.ca.location': 'ca.pem'
}, {}, {'topics': ['demo-topic']});
stream.on('data', function(message) {
console.log('Got message');
console.log(message.value.toString());
});