Last active
May 30, 2023 03:51
-
-
Save armando-couto/966a3abd18b0c864aa79a94f269d12f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'kafka' | |
# Set up Kafka client | |
kafka = Kafka.new( | |
seed_brokers: ['localhost:9092'], | |
client_id: 'my_kafka_client' | |
) | |
# Create a producer | |
producer = kafka.producer | |
# Create a consumer | |
consumer = kafka.consumer(group_id: 'my_consumer_group') | |
# Define the topic | |
topic = 'my_topic' | |
# Publish messages to Kafka | |
producer.produce('Hello, Kafka!', topic: topic) | |
producer.deliver_messages | |
# Subscribe to the topic and consume messages | |
consumer.subscribe(topic) | |
# Start consuming messages | |
consumer.each_message do |message| | |
puts "Received message: #{message.value}" | |
end | |
# Close connections | |
producer.shutdown | |
consumer.stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment