- Published
- Author
- Nitturu BabaSystem Analyst
Real-time AI response streaming improves user experience by reducing wait times and making interactions feel more dynamic. Instead of waiting for the entire response to be generated before displaying it, streaming allows data to be processed and presented incrementally.
Example of AI response streaming using Nest Js backend and Next JS front end.
Setting Up the NestJS Backend for Streaming AI Responses
Controller
How It Works
• The @Post('chat') endpoint listens for chat requests.
• The streamText function sends user messages to OpenAI and receives a streamed response.
• pipeDataStreamToResponse(res) directly streams the AI-generated content to the client as it arrives.
Building the Next.js Frontend for AI Response Streaming
chat/page.tsx
How It Works
• The useChat hook from AI-SDK manages state and streaming logic automatically.
• It sends user messages to the backend and updates the UI in real time as responses arrive.
• The messages array dynamically updates, displaying each chunk of AI-generated text as it's received.
#C08DPTN3JAW #streaming #next js #nest js
Example of AI response streaming using Nest Js backend and Next JS front end.
Setting Up the NestJS Backend for Streaming AI Responses
Controller
TypeScript
import { Controller, Post, Body, Res } from '@nestjs/common';
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
import { Response } from 'express';
@Controller('orchestrator')
export class OrchestratorController {
@Post('chat')
async chat(@Body() payload: any, @Res() res: Response) {
const { messages } = payload;
const result = streamText({
model: openai('gpt-4o'),
messages,
});
result.pipeDataStreamToResponse(res); // Streams the AI response directly to the client
}
}How It Works
• The @Post('chat') endpoint listens for chat requests.
• The streamText function sends user messages to OpenAI and receives a streamed response.
• pipeDataStreamToResponse(res) directly streams the AI-generated content to the client as it arrives.
Building the Next.js Frontend for AI Response Streaming
chat/page.tsx
JavaScript
'use client';
import { useChat } from '@ai-sdk/react';
export default function Home() {
const { messages, input, handleInputChange, handleSubmit } = useChat({
api: 'https://localhost:3000/api/orchestrator/chat', // make the post request to the NestJS backend
});
return (
<div>
{messages.map((message) => (
<div key={message.id}>
{message.role === 'user' ? 'User: ' : 'AI: '}
{message.content}
</div>
))}
<form onSubmit={handleSubmit}>
<input
name="prompt"
value={input}
onChange={handleInputChange}
className="text-black"
/>
<button type="submit">Submit</button>
</form>
</div>
);
}How It Works
• The useChat hook from AI-SDK manages state and streaming logic automatically.
• It sends user messages to the backend and updates the UI in real time as responses arrive.
• The messages array dynamically updates, displaying each chunk of AI-generated text as it's received.
#C08DPTN3JAW #streaming #next js #nest js