Vercel stream keep alive???

I just found this git repo for publishing messages with NextJS and Upstash and I got some questions. https://github.com/rishi-raj-jain/upstash-nextjs-publish-messages-with-sse-example/blob/master/app/api/stream/route.js
// Can be 'nodejs', but Vercel recommends using 'edge'
export const runtime = 'nodejs'

// Prevents this route's response from being cached
export const dynamic = 'force-dynamic'

// Use ioredis to subscribe
import Redis from 'ioredis'

// Define the key to listen and publish messages to
const setKey = 'posts'

// Create a redis subscriber
const redisSubscriber = new Redis(process.env.UPSTASH_REDIS_URL)

export async function GET() {
const encoder = new TextEncoder()
// Create a stream
const customReadable = new ReadableStream({
start(controller) {
// Subscribe to Redis updates for the key: "posts"
// In case of any error, just log it
redisSubscriber.subscribe(setKey, (err) => {
if (err) console.log(err)
})
// Listen for new posts from Redis
redisSubscriber.on('message', (channel, message) => {
// Send data with the response in the SSE format
// Only send data when the channel message is reeived is same as the message is published to
if (channel === setKey) controller.enqueue(encoder.encode(`data: ${message}\n\n`))
})
},
})
// Return the stream and try to keep the connection alive
return new Response(customReadable, {
// Set headers for Server-Sent Events (SSE) / stream from the server
headers: { 'Content-Type': 'text/event-stream; charset=utf-8', Connection: 'keep-alive', 'Cache-Control': 'no-cache, no-transform', 'Content-Encoding': 'none' },
})
}
// Can be 'nodejs', but Vercel recommends using 'edge'
export const runtime = 'nodejs'

// Prevents this route's response from being cached
export const dynamic = 'force-dynamic'

// Use ioredis to subscribe
import Redis from 'ioredis'

// Define the key to listen and publish messages to
const setKey = 'posts'

// Create a redis subscriber
const redisSubscriber = new Redis(process.env.UPSTASH_REDIS_URL)

export async function GET() {
const encoder = new TextEncoder()
// Create a stream
const customReadable = new ReadableStream({
start(controller) {
// Subscribe to Redis updates for the key: "posts"
// In case of any error, just log it
redisSubscriber.subscribe(setKey, (err) => {
if (err) console.log(err)
})
// Listen for new posts from Redis
redisSubscriber.on('message', (channel, message) => {
// Send data with the response in the SSE format
// Only send data when the channel message is reeived is same as the message is published to
if (channel === setKey) controller.enqueue(encoder.encode(`data: ${message}\n\n`))
})
},
})
// Return the stream and try to keep the connection alive
return new Response(customReadable, {
// Set headers for Server-Sent Events (SSE) / stream from the server
headers: { 'Content-Type': 'text/event-stream; charset=utf-8', Connection: 'keep-alive', 'Cache-Control': 'no-cache, no-transform', 'Content-Encoding': 'none' },
})
}
Isn't this going to timeout or use a shit amount of function execution? What happend when deploy to the edge? Does this even work without the bill going to the moon?
GitHub
upstash-nextjs-publish-messages-with-sse-example/app/api/stream/rou...
A demo of Server Side Events publishing with Next.js Server Actions and Upstash, deployed to Vercel. - rishi-raj-jain/upstash-nextjs-publish-messages-with-sse-example
0 Replies
No replies yetBe the first to reply to this messageJoin