# Read messages from the last x seconds of a topic

**URL:** https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407
**Category:** Clients
**Created:** [16 March 2024 08:32 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407 "2024-03-16T08:32:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mdarende](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@mdarende](https://forum.confluent.io/u/mdarende)
#### Post date: [16 March 2024 08:32 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407/1 "2024-03-16T08:32:08Z")

</div>

Lets say 30 producers are sending data to the same Kafka topic every 2 seconds at the same time. From my C# code I want to read cyclic (every 2 seconds) the messages only from the last 2 seconds. How can I do that? I tried a lot but couldn’t succeed. Could you share a very simple sample code?

---

<div class="post-metadata">

### Author: ![dtroiano](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/dtroiano/32/1961_2.png) [@dtroiano](https://forum.confluent.io/u/dtroiano)
#### Post date: [18 March 2024 17:30 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407/2 "2024-03-18T17:30:44Z")

</div>

You can accomplish this by getting the offsets corresponding to the desired timestamp (2 seconds ago) via [OffsetsForTimes](https://docs.confluent.io/platform/current/clients/confluent-kafka-dotnet/_site/api/Confluent.Kafka.IConsumer-2.html#Confluent_Kafka_IConsumer_2_OffsetsForTimes_System_Collections_Generic_IEnumerable_Confluent_Kafka_TopicPartitionTimestamp__System_TimeSpan_), and then seeking to those offsets:

```auto
consumer.Subscribe(topic);

...

var seekTimestamp = new Timestamp(DateTime.Now.Subtract(TimeSpan.FromSeconds(2)));
var offsets = consumer.OffsetsForTimes(
    consumer.Assignment.Select(partition => new TopicPartitionTimestamp(partition, seekTimestamp)),
    /* insert TimeSpan timeout */);

foreach (var offset in offsets) {
    consumer.Seek(offset);
}

...

while (true) {
    var cr = consumer.Consume(cts.Token);
    ...
}

```

---

<div class="post-metadata">

### Author: ![mdarende](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@mdarende](https://forum.confluent.io/u/mdarende)
#### Post date: [19 March 2024 09:22 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407/3 "2024-03-19T09:22:51Z")

</div>

Hello I have completed the code, according to your answer as following. But I think I make something wrong. My goal is to read every two seconds the messages of the last two seconds. That would be in ideal case 30 messages (from 30 consumers that produce data at the same time to the same topic). But I get every 2 seconds just one message with my code. The reason is clear. For every while loop I get one result. What could be wrong in my code?

```
    public async Task Read_data_from_Kafka()
    {
        await Task.Delay(1);
        string item_value = "";
        string inventar = "";
        string timestamp = "";
        config = new ConsumerConfig()
        {
            BootstrapServers = servers,
            GroupId = "foo",
            AutoOffsetReset = AutoOffsetReset.Latest,                
            EnableAutoCommit = false,
        };
        
        consumer_2 = new ConsumerBuilder<Ignore, string>(config).Build();

        TopicPartition topicPartition_0 = new TopicPartition("my_topic", new Partition(0));
        
        consumer_2.Subscribe("my_topic");

        var seekTimestamp = new Timestamp(DateTime.Now.Subtract(TimeSpan.FromSeconds(2)));
        var offsets = consumer_2.OffsetsForTimes(
        consumer_2.Assignment.Select(partition => 
        
        new TopicPartitionTimestamp(topicPartition_0, seekTimestamp)), TimeSpan.FromMilliseconds(10000));
          

        foreach (var offset in offsets)
        {
            consumer_2.Seek(offset);
        }            

        while (true)
        {
            var consumeResult = consumer_2.Consume(CancellationToken.None);
            using (StreamWriter sw = File.AppendText("D:\\test\\kafka_messages.txt"))
            {                    
                sw.WriteLine("Kafka message: " + consumeResult.Message.Value + " " + Convert.ToString(DateTime.Now));
            }
            Thread.Sleep(2000);
            //break;
        }
    }

```

---

<div class="post-metadata">

### Author: ![dtroiano](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/dtroiano/32/1961_2.png) [@dtroiano](https://forum.confluent.io/u/dtroiano)
#### Post date: [19 March 2024 15:16 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407/4 "2024-03-19T15:16:18Z")

</div>

> [@mdarende](#):
>
> For every while loop I get one result

This is expected - in each iteration you’re calling `Consume` which just gets one result. You’d need to keep going. There isn’t a single method you can call to get all within a time window.

> [@mdarende](#):
>
> My goal is to read every two seconds the messages of the last two seconds.

To do this and avoid dupes, you’d need a block of code that seeks to two seconds ago and stops when the message timestamps go beyond the given two second window. And then it gets tricky to handle the case where the consumer can’t keep up. Are you _sure_ you need to consume in this 2 second window way? Why not avoid the 2 second windows and just consume as events come in? You wouldn’t have to worry about tracking state, carefully handling the 2 second window boundaries to avoid dupes / skipping messages, and handling the case where the consumer gets behind.

---

<div class="post-metadata">

### Author: ![mdarende](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@mdarende](https://forum.confluent.io/u/mdarende)
#### Post date: [20 March 2024 12:43 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407/5 "2024-03-20T12:43:26Z")

</div>

Hello Dave  
Thank you for the great suggestion. Sorry, as I am recently working with Kafka, I would ask how I could realise in this case the event based consuming. Could you give a very basic example for that? I mean how will I be informed of an event , so that I can just consume in this case.

Thanks in advance

---

<div class="post-metadata">

### Author: ![dtroiano](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/dtroiano/32/1961_2.png) [@dtroiano](https://forum.confluent.io/u/dtroiano)
#### Post date: [20 March 2024 14:17 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407/6 "2024-03-20T14:17:40Z")

</div>

> [@mdarende](#):
>
> how will I be informed of an event , so that I can just consume in this case.

Streaming apps often keep running, i.e., you would `Consume` in a loop. The consumer is always “on” and continuously consumes. In this case, there’s no need to separately be informed of an event and then subsequently consume it. You subscribe and consume, and depending on the `Consume` method you’re using, you can either have it return if no results are available within a time span, or wait until canceled.

It doesn’t always have to be this “run forever” way though. What kind of application are you working on and what kind of event produce / consume pattern goes with it?

---

<div class="post-metadata">

### Author: ![mdarende](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@mdarende](https://forum.confluent.io/u/mdarende)
#### Post date: [20 March 2024 15:26 UTC](https://forum.confluent.io/t/read-messages-from-the-last-x-seconds-of-a-topic/10407/7 "2024-03-20T15:26:27Z")

</div>

hello Dave

I have explained my use case in the following topic:

> [@Consuming data from Kafka topic for a leight weight Live-Chart app. in .NET](https://forum.confluent.io/t/consuming-data-from-kafka-topic-for-a-leight-weight-live-chart-app-in-net/10421):
>
> Hello In my lightweigt C# .NET app I have to visualise the data, of a Kafka topic in a simple live chart. Below I have listed the native requirements of my use case. Accordingly to that I am searching an easy way for consuming this “native-live” data from the Kafka topic to my C# app. appr. 40 producers are sending data to the same Kafka topic (with 3 partitions) cyclic every second. One json telegram of each producer is containing 15 keys. I have to visualise 4-5 of this keys (-values). When…
