Vercel stream keep alive???
Answered
Bonga shad posted this in #help-forum
Bonga shadOP
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
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?
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' },
})
}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?
Answered by Ray
Isn't this going to timeout or use a shit amount of function execution?it will timeout on vercel or other serverless platform after the limit
What happend when deploy to the edge?edge doesn't support ioredis
3 Replies
@Bonga shad 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
typescript
// 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?
Isn't this going to timeout or use a shit amount of function execution?it will timeout on vercel or other serverless platform after the limit
What happend when deploy to the edge?edge doesn't support ioredis
Answer
@Bonga shad good to know. thanks dude!
it should work when it is self hosting on a server