Skip to content

Event Iterator in oRPC Clients

An Event Iterator in oRPC behaves like an AsyncGenerator. Simply iterate over it and await each event.

Basic Usage

ts
const 
iterator
= await
client
.
streaming
()
for await (const
event
of
iterator
) {
console
.
log
(
event
.
message
)
}

Stopping the Stream Manually

Call .return() on the iterator to gracefully end the stream.

ts
const iterator = await client.streaming()

for await (const event of iterator) {
  if (wantToStop) {
    await iterator.return()
    break
  }

  console.log(event.message)
}

Error Handling

INFO

Unlike traditional SSE, the Event Iterator does not automatically retry on error. To enable automatic retries, refer to the Client Retry Plugin.

ts
const iterator = await client.streaming()

try {
  for await (const event of iterator) {
    console.log(event.message)
  }
}
catch (error) {
  if (error instanceof ORPCError) {
    // Handle the error here
  }
}

INFO

Errors thrown by the server can be instances of ORPCError.

Released under the MIT License.