Streaming help with dazl

I’m hoping to connect an external system to listen in on contracts being created/updated through dazl and the ledger API so that I can, for example, send a notification/email when something occurs.

Looking at the docs here dazl.ledger and the examples of opening up a stream

# asynchronous connections
async with conn.stream() as stream:
   async for event in stream.creates():
      # print every contract create...forever
      print(event.contract_id, event.payload)

I have a few questions:

  1. All the streams seem to open up from the beginning, is there a way to skip to a certain point? Once we have years of data, there needs to be a way to skip to today, the latest or only new ones from here on out
  2. Is registering callbacks still a good pattern to use?
# registering a callback using a decorator
@stream.on_create("My:Tmpl")
def handle(event):
    conn.exercise(event.cid, "Accept")
  1. If dazl doesn’t have this functionality, is using the Ledger API directly an option? Does the grpc API have a way to handle question 1
  1. .stream() takes an offset parameter, so you can simply pass that:

    async with conn.stream(offset="...") as stream:
        async for event in stream.creates():
            ...
    

    But because you’d need to also get offsets, you’d need to use a slightly different API:

    async with conn.stream(offset="...") as stream:
        async for event in stream:
            if isinstance(event, CreateEvent):
                ...
            elif isinstance(event, Boundary):
                # remember this offset somehow
                ... = event.offset
    
  2. Both are supported; use whichever style you prefer. (I personally don’t use the callback style any more.)

  3. Not sure I understand the question…is this simply a repeat of 1?

We can ignore 3 since it’s possible with dazl! thanks!

For the offset, how does one get one? does the end of a stream automatically send a Boundary event?

See the above code sample… Boundary events are mixed in with corresponding CreateEvent's. If you’ve used the HTTP JSON API server’s websocket API, this will look familiar as it is essentially the same pattern.

1 Like

I understand the code sample and that Boundary events are mixed in to the stream, I’m just wondering what is the pattern? When will the API send a Boundary event? For example, when it hits the end of the current possible stream of events? every 10 events?

Trying this out myself, it does give a Boundary event at the end of a stream