T
tRPC

Use RxJS Observable for subscription procedure

Use RxJS Observable for subscription procedure

Bb0o11/16/2023
On the server, I have an RxJS Observable that I'd like to use for one of my subscription procedures. Is this possible without writing a converter function that takes an RxJS Observable and converts it to a tRPC Observable? For example, what I'd like to do looks something like this:
import { Observable } from 'rxjs'
import { authedProcedure, createTRPCRouter } from '../../api/trpc'

export const demoRouter = createTRPCRouter({
fizzBuzz: authedProcedure.subscription(() =>
new Observable<string>((subscriber) => {
let i = 1
const interval = setInterval(() => {
if (i % 15 === 0) {
subscriber.next('FizzBuzz')
} else if (i % 3 === 0) {
subscriber.next('Fizz')
} else if (i % 5 === 0) {
subscriber.next('Buzz')
} else {
subscriber.next(String(i))
}
i++
}, 1000)
return () => clearInterval(interval)
})
),
})
import { Observable } from 'rxjs'
import { authedProcedure, createTRPCRouter } from '../../api/trpc'

export const demoRouter = createTRPCRouter({
fizzBuzz: authedProcedure.subscription(() =>
new Observable<string>((subscriber) => {
let i = 1
const interval = setInterval(() => {
if (i % 15 === 0) {
subscriber.next('FizzBuzz')
} else if (i % 3 === 0) {
subscriber.next('Fizz')
} else if (i % 5 === 0) {
subscriber.next('Buzz')
} else {
subscriber.next(String(i))
}
i++
}, 1000)
return () => clearInterval(interval)
})
),
})

Looking for more? Join the community!

T
tRPC

Use RxJS Observable for subscription procedure

Join Server