Replies: 1 comment
-
oRPC doesn't have a built-in way to forward SSE, but you can implement something like this. const watchEvents = base.threads.watchEvents.use(requireAuth)
.input(z.object({ threadId: ThreadId.validator }))
.output(eventIterator(z.object({ type: z.string(), content: z.string() })))
.handler(async function* ({ context, input, errors, signal }) {
// get DO instance somehow...
const threadInstance = getMyDurableObjectInstance()
try {
const eventSource = new EventSource('http://do/stream-events', { fetcher: threadInstance })
const pending: any[] = []
let pendingResolve: ((value: any) => void) | undefined
eventSource.onmessage = function (event) {
if (pendingResolve) {
pendingResolve(JSON.parse(event.data))
}
else {
pending.push(JSON.parse(event.data))
}
}
while (true) {
if (pending.length > 0) {
yield pending.shift()
}
else {
yield await new Promise((resolve) => {
pendingResolve = resolve
})
}
}
}
finally {
console.log('Aborting SSE stream')
// controller.abort();
}
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First off, I've been loving orpc - awesome work!
I've got a cloudflare durable object that returns an SSE stream at
/stream-events
. I was hoping to wrap the do and proxy the stream back through orpc, so that I can leverage a) my existing orpc client and b) my existing orpc middleware hooks for auth, db connections, etc.Basically the below (question in comments). Thank you in advance for any tips!
Beta Was this translation helpful? Give feedback.
All reactions