Streaming Mode
Streaming Mode enables real-time, character-by-character display of AI responses using Server-Sent Events (SSE), creating a more dynamic and engaging chat experience similar to modern AI interfaces like ChatGPT.Overview
When streaming mode is enabled, instead of waiting for the complete response, messages appear progressively as they are generated by the AI model. This provides immediate feedback and a more natural conversation flow.⚡ Real-time Display
Characters appear as they’re generated, not all at once
🌊 Server-Sent Events
Uses SSE for efficient, persistent connections
📱 Better UX
Immediate feedback and reduced perceived latency
🔧 Easy Setup
Simple prop to enable streaming functionality
Enabling Streaming Mode
Enable streaming mode with a single prop:Streaming vs Regular Mode
Understanding the differences between streaming and regular chat modes:Feature | Regular Mode | Streaming Mode |
---|---|---|
Response Display | Complete message appears at once | Character-by-character display |
API Endpoint | POST /chat | POST /chat/stream |
Connection Type | Single HTTP request | Server-Sent Events |
Visual Feedback | Loading spinner | Real-time typing effect |
Usage Tracking | Full cost information | Limited cost tracking |
Perceived Speed | Slower (wait for complete response) | Faster (immediate feedback) |
Network Usage | Lower (single request) | Higher (persistent connection) |
Technical Requirements
Server-Side Implementation
Your streaming endpoint must support Server-Sent Events and return data in the expected format:Endpoint Requirements
POST /api/v1/ai/projects/{projectId}/chat/stream
Request:
application/json
, Response: text/event-stream
text/event-stream
Response Format
The endpoint should return SSE events in this specific format:Stream Event Types
Partial message content chunk
Indicates stream completion with optional session ID
Advanced Streaming Features
Streaming with ChatWidget
Combine streaming with the floating widget for optimal user experience:Streaming with Function Calls
Function calls work seamlessly with streaming mode:Connection Status Monitoring
Monitor streaming connection status and handle errors:Performance Considerations
Network Optimization
Connection Management: Streaming uses persistent connections. Ensure your server can handle multiple concurrent SSE connections.
Compression: Enable gzip compression on your server for SSE responses to reduce bandwidth usage.
Client-Side Optimization
Troubleshooting
Common Issues
Streaming Not Working
Streaming Not Working
Symptoms: Messages appear all at once instead of streamingSolutions:
- Verify your server endpoint returns
text/event-stream
content type - Check that your proxy correctly forwards the
Accept: text/event-stream
header - Ensure your server doesn’t buffer the SSE response
Connection Drops
Connection Drops
Symptoms: Streaming stops mid-responseSolutions:
- Implement connection retry logic on both client and server
- Check for network timeouts and adjust server keep-alive settings
- Monitor for proxy/load balancer timeouts in production
High Memory Usage
High Memory Usage
Symptoms: Browser memory increases during long streaming sessionsSolutions:
- Implement message history limits
- Close unused SSE connections properly
- Clear old messages from memory periodically
CORS Issues
CORS Issues
Symptoms: Cross-origin errors in browser consoleSolutions:
- Add proper CORS headers to your streaming endpoint
- Include
Access-Control-Allow-Origin
and other necessary headers - Test with same-origin requests first to isolate CORS issues
Debugging Tips
Enable detailed logging for streaming debugging:Browser Compatibility
Streaming mode uses modern web APIs with broad browser support:- Server-Sent Events: Supported in all modern browsers (IE 11+)
- EventSource API: Native support across all target browsers
- Graceful Degradation: Automatically falls back to regular mode if streaming fails
Best Practices
Error Handling: Always implement proper error handling for streaming connections, as they can be interrupted by network issues.
User Feedback: Consider adding visual indicators when streaming is active vs. when it’s complete.
Resource Management: Ensure SSE connections are properly closed when components unmount to prevent memory leaks.
Streaming mode uses more network resources than regular mode. Consider offering users the choice between modes for bandwidth-conscious applications.