Sploopy
Sploopy
TtRPC
Created by Donald Biden on 3/25/2025 in #❓-help
A simple example of using SSE and subscriptions
export const SubscriptionComponent = () => {
trpc.SubscriptionRouter.SubscriptionEndpoint.useSubscription(
//Props here
{},
{
onData: (data) => {
//Do the thing with the data
},
})
}
export const SubscriptionComponent = () => {
trpc.SubscriptionRouter.SubscriptionEndpoint.useSubscription(
//Props here
{},
{
onData: (data) => {
//Do the thing with the data
},
})
}
5 replies
TtRPC
Created by Donald Biden on 3/25/2025 in #❓-help
A simple example of using SSE and subscriptions
export const SubscriptionEndpoint = publicProcedure
.subscription(async function* ({ _input, _ctx }) {
const redisSubscriber = new Redis(...)
const emitter = new EventEmitter()

try {
//Optional if you want to use redis
await redisSubscriber.subscribe("some_channel_name")
//Redis listening for messages
redisSubscriber.on("message", async () => {
//If you don't want to use Redis you can just use an event emitter alone
emitter.emit("update", subData)
})

//Async generator to keep the thread alive
for await (const output of on(emitter, "update")) {
//Yield the output to your frontend subscribers
yield output[0]
}
} catch (error) {
throw error
} finally {
redisSubscriber.unsubscribe("some_channel_name")
redisSubscriber.quit()
}
})
export const SubscriptionEndpoint = publicProcedure
.subscription(async function* ({ _input, _ctx }) {
const redisSubscriber = new Redis(...)
const emitter = new EventEmitter()

try {
//Optional if you want to use redis
await redisSubscriber.subscribe("some_channel_name")
//Redis listening for messages
redisSubscriber.on("message", async () => {
//If you don't want to use Redis you can just use an event emitter alone
emitter.emit("update", subData)
})

//Async generator to keep the thread alive
for await (const output of on(emitter, "update")) {
//Yield the output to your frontend subscribers
yield output[0]
}
} catch (error) {
throw error
} finally {
redisSubscriber.unsubscribe("some_channel_name")
redisSubscriber.quit()
}
})
5 replies
TtRPC
Created by Donald Biden on 3/25/2025 in #❓-help
A simple example of using SSE and subscriptions
Which part are you confused on? The
.subscription
.subscription
trpc component or using it on the frontend?
5 replies