github unnoq/orpc v1.9.0

15 hours ago

Durable Iterator docs

Durable Iterator is a complete rewrite of the old Durable Event Iterator. It builds on Event Iterator by offloading streaming to a dedicated service that delivers durable event streams with automatic reconnections and event recovery.

Note

While not limited to Cloudflare Durable Objects, it's currently the only supported implementation.

Durable Object

import { DurableIteratorObject } from '@orpc/experimental-durable-iterator/durable-object'

export class ChatRoom extends DurableIteratorObject<{ message: string }> {
  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env, {
      signingKey: 'secret-key', // Replace with your actual signing key
      interceptors: [
        onError(e => console.error(e)), // log error thrown from rpc calls
      ],
      onSubscribed: (websocket, lastEventId) => {
        console.log(`WebSocket Ready id=${websocket['~orpc'].deserializeId()}`)
      }
    })
  }

  someMethod() {
    // publishEvent method inherited from DurableIteratorObject
    this.publishEvent({ message: 'Hello, world!' })
  }
}

Server Side

import { DurableEventIterator } from '@orpc/experimental-durable-iterator'

export const router = {
  onMessage: base.handler(({ context }) => {
    return new DurableEventIterator<ChatRoom>('some-room', {
      tags: ['tag1', 'tag2'],
      signingKey: 'secret-key', // Replace with your actual signing key
    })
  }),

  sendMessage: base
    .input(z.object({ message: z.string() }))
    .handler(async ({ context, input }) => {
      const id = context.env.CHAT_ROOM.idFromName('some-room')
      const stub = context.env.CHAT_ROOM.get(id)

      await stub.publishEvent(input)
    }),
}

Client Side

const iterator = await client.onMessage()

for await (const { message } of iterator) {
  console.log('Received message:', message)
}

await client.sendMessage({ message: 'Hello, world!' })

experimental_streamedOptions is removed in old tanstack query

If you depend on this, please try NEW Tanstack Query Interagration


Tip

If you find oRPC valuable and would like to support its development, you can do so here.

   🚀 Features

    View changes on GitHub

Don't miss a new orpc release

NewReleases is sending notifications on new releases.