Skip to content

Instantly share code, notes, and snippets.

@armando-couto
Last active May 30, 2023 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armando-couto/966a3abd18b0c864aa79a94f269d12f3 to your computer and use it in GitHub Desktop.
Save armando-couto/966a3abd18b0c864aa79a94f269d12f3 to your computer and use it in GitHub Desktop.
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