NinjaBunny
NinjaBunny
Explore posts from servers
TTCTheo's Typesafe Cult
Created by NinjaBunny on 1/15/2025 in #questions
real time video stream that gets served to multiple users
I tried HLS in the past, but it wasn't working because I was not interacting with the browser lol and I forgot I was trying to have it auto play when I was using HLS the first time
10 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 1/15/2025 in #questions
real time video stream that gets served to multiple users
actually taht's exaclty what I did today at work because I found out earlier to day that I am unable to use mp4 entirely due to the first chunk or 2 having data regarding the actual video stream
10 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 1/15/2025 in #questions
real time video stream that gets served to multiple users
I don't really think this is a backend issue at all and is a very niche issue on the browser dealing with the media source and handling video chunks properly
10 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 1/15/2025 in #questions
real time video stream that gets served to multiple users
// stream/[id]/route.ts
let connection: Connection | null = null;
let channel: Channel | null = null;

export async function GET(
req: NextRequest,
{ params }: { params: { id: string } },
) {
// gets or refreshes user tokens or throws response error
const tokens = await getTokens(req);

if (tokens instanceof NextResponse) return tokens;

channel = await getChannel();

const exchange = `stream.${params.id}`;
const session = nanoid();
const queueName = `${exchange}.${session}`;

try {
await channel.assertExchange(exchange, "fanout", { durable: false });

const { queue } = await channel.assertQueue(queueName, {
exclusive: true,
autoDelete: true,
});
await channel.bindQueue(queue, exchange, "");

const stream = new ReadableStream({
start(controller) {
channel?.consume(queue, (message) => {
if (message) {
controller.enqueue(message.content);
}
});
},
async cancel() {
await channel?.cancel(queue);

await cleanup();
},
});

const headers = new Headers();

headers.set("Content-Type", "text/event-stream");
headers.set("Transfer-Encoding", "chunked");

const response = new NextResponse(stream, {
headers,
});

if (typeof tokens.data !== "string") {
response.cookies.set("access_token", tokens.data.at.value, {
...DEFAULT_COOKIE_OPTIONS,
expires: tokens.data.at.expires,
});

response.cookies.set("refresh_token", tokens.data.rt.value, {
...DEFAULT_COOKIE_OPTIONS,
expires: tokens.data.rt.expires,
});
}

return response;
} catch (error) {
console.error("Error in stream setup", error);

return NextResponse.json(
{ success: false, error: "Failed to start stream" } as const,
{ status: 500 },
);
}
}
// stream/[id]/route.ts
let connection: Connection | null = null;
let channel: Channel | null = null;

export async function GET(
req: NextRequest,
{ params }: { params: { id: string } },
) {
// gets or refreshes user tokens or throws response error
const tokens = await getTokens(req);

if (tokens instanceof NextResponse) return tokens;

channel = await getChannel();

const exchange = `stream.${params.id}`;
const session = nanoid();
const queueName = `${exchange}.${session}`;

try {
await channel.assertExchange(exchange, "fanout", { durable: false });

const { queue } = await channel.assertQueue(queueName, {
exclusive: true,
autoDelete: true,
});
await channel.bindQueue(queue, exchange, "");

const stream = new ReadableStream({
start(controller) {
channel?.consume(queue, (message) => {
if (message) {
controller.enqueue(message.content);
}
});
},
async cancel() {
await channel?.cancel(queue);

await cleanup();
},
});

const headers = new Headers();

headers.set("Content-Type", "text/event-stream");
headers.set("Transfer-Encoding", "chunked");

const response = new NextResponse(stream, {
headers,
});

if (typeof tokens.data !== "string") {
response.cookies.set("access_token", tokens.data.at.value, {
...DEFAULT_COOKIE_OPTIONS,
expires: tokens.data.at.expires,
});

response.cookies.set("refresh_token", tokens.data.rt.value, {
...DEFAULT_COOKIE_OPTIONS,
expires: tokens.data.rt.expires,
});
}

return response;
} catch (error) {
console.error("Error in stream setup", error);

return NextResponse.json(
{ success: false, error: "Failed to start stream" } as const,
{ status: 500 },
);
}
}
10 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 1/15/2025 in #questions
real time video stream that gets served to multiple users
10 replies
DTDrizzle Team
Created by NinjaBunny on 3/10/2024 in #help
Programmatically get columns from table
I don't think this applies to relational queries as well
3 replies
DTDrizzle Team
Created by NinjaBunny on 7/1/2023 in #help
Error when trying to generate a migration schema
I’m not 100% sure if that was the cause of it, but I know it magically started working again once I updated the package for drizzle-kit
6 replies
DTDrizzle Team
Created by NinjaBunny on 7/1/2023 in #help
Error when trying to generate a migration schema
It happened a bit ago, but I think that’s what was wrong with the version of drizzle-kit
6 replies
DTDrizzle Team
Created by NinjaBunny on 7/1/2023 in #help
Error when trying to generate a migration schema
There was something wrong with that version of drizzle if I’m not mistaken
6 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
I appreciate it
21 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
Thank you for the help 🙂
21 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
nope the websocket server is from a different microservice, I just thought it would be cool if we can revalidate the path like that, but it makes sense
21 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
or something marked as "use client"
21 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
even if it's on a client component?
21 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
yea I know, but I'm forced into using websockets because I need a constant data feed for certain parts of my teams application. dealing with realtime events and stuff
21 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
I had a feeling I would have to use a client component, but it would be cool to do something like this on the server
21 replies
TTCTheo's Typesafe Cult
Created by NinjaBunny on 10/26/2023 in #questions
WebSocket on Server Component
ahhh okay thanks
21 replies
DTDrizzle Team
Created by Amur on 8/22/2023 in #help
DB connections hangs after successful execution in Lambda locally
any updates on when this feature will be added?
22 replies
DTDrizzle Team
Created by Nicolas on 9/23/2023 in #help
Neon and Drizzle ORM: Can my schema.ts create my tables in Neon?
I believe so, if that's wrong please let me know
7 replies
DTDrizzle Team
Created by Nicolas on 9/23/2023 in #help
Neon and Drizzle ORM: Can my schema.ts create my tables in Neon?
find the introspection tool
7 replies