Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the last event from an Event Store stream with only one HTTP request?

All the docs I can find seem to suggest I need two http requests to do this: one to the stream, giving me a link to the last event, and then one to follow that link.

That seems bizarre, isn't there a way to do this with just one request?

like image 607
alksdjg Avatar asked Mar 08 '23 14:03

alksdjg


1 Answers

If using the .NET Client you can also use the read backwards API

Task<StreamEventsSlice> ReadStreamEventsBackwardAsync(string stream, int start, int count, bool resolveLinkTos)

Where

  • Start - Generally if you know which event number to start from but in this case we do not know so we can StreamPosition.End (-1) to start from the end.
  • Count - How many events to get going backwards

So this code will get you the last event on the stream (with linkTos enabled if its a projection)

 StreamEventsSlice slice = await Connection.ReadStreamEventsBackwardAsync("StreamName", StreamPosition.End, 1, true, creds);
like image 166
Piotr Kula Avatar answered Apr 07 '23 01:04

Piotr Kula