SUPABASE_USAGE_GUIDE.md•6.5 kB
# 🚀 Supabase Free Tier Usage Guide
This guide shows you how to effectively use Supabase's free tier in your MCP Chat Support System.
## 📊 Free Tier Limits & Usage
### ✅ What You Get (Free)
- **Database**: 500MB storage
- **Bandwidth**: 2GB transfer/month
- **Auth**: 50,000 monthly active users
- **Storage**: 1GB file storage
- **Edge Functions**: 500,000 invocations/month
- **Real-time**: Unlimited connections
### 🎯 Optimizing for Free Tier
#### 1. Database Storage (500MB)
**Current Usage**: ~50-100MB for typical chat app
**Optimization Tips**:
- Use efficient data types (TEXT vs VARCHAR)
- Archive old messages after 30 days
- Compress JSON data in context fields
- Regular cleanup of unused data
```sql
-- Archive old messages (run monthly)
INSERT INTO archived_messages
SELECT * FROM chat_messages
WHERE created_at < NOW() - INTERVAL '30 days';
DELETE FROM chat_messages
WHERE created_at < NOW() - INTERVAL '30 days';
```
#### 2. Bandwidth (2GB/month)
**Current Usage**: ~100-200MB/month for typical usage
**Optimization Tips**:
- Enable compression in API responses
- Use pagination for large data sets
- Cache frequently accessed data
- Optimize image uploads
#### 3. Authentication (50,000 MAU)
**Current Usage**: ~1,000-5,000 MAU for typical app
**Optimization Tips**:
- Implement session management
- Use refresh tokens efficiently
- Monitor active user sessions
#### 4. Storage (1GB)
**Current Usage**: ~100-300MB for typical usage
**Optimization Tips**:
- Compress images before upload
- Set file size limits
- Use CDN for static assets
- Regular cleanup of unused files
## 🔧 Implementation Examples
### 1. Real-time Chat (Most Important Feature)
```typescript
// Using the useChat hook we created
import { useChat } from '../hooks/useChat'
function ChatComponent() {
const { messages, sendMessage, loading } = useChat(tenantId)
const handleSend = async (message: string) => {
await sendMessage(message, userId)
// Message appears instantly via real-time subscription!
}
return (
<div>
{messages.map(msg => (
<Message key={msg.id} message={msg} />
))}
<MessageInput onSend={handleSend} />
</div>
)
}
```
### 2. Authentication Flow
```typescript
// Using the useAuth hook we created
import { useAuth } from '../hooks/useAuth'
function LoginComponent() {
const { signIn, signInWithGoogle, user, loading } = useAuth()
const handleGoogleLogin = async () => {
const { error } = await signInWithGoogle()
if (!error) {
// User is automatically logged in
// Profile is fetched automatically
}
}
return (
<button onClick={handleGoogleLogin}>
Sign in with Google
</button>
)
}
```
### 3. File Upload with Storage
```typescript
import { supabase } from '../lib/supabase'
const uploadFile = async (file: File, bucket: string = 'chat-files') => {
const fileExt = file.name.split('.').pop()
const fileName = `${Math.random()}.${fileExt}`
const filePath = `${bucket}/${fileName}`
const { data, error } = await supabase.storage
.from(bucket)
.upload(filePath, file, {
cacheControl: '3600',
upsert: false
})
if (error) throw error
// Get public URL
const { data: { publicUrl } } = supabase.storage
.from(bucket)
.getPublicUrl(filePath)
return publicUrl
}
```
## 📈 Monitoring Usage
### 1. Supabase Dashboard
- Go to your project dashboard
- Check **Usage** tab for current usage
- Set up alerts for approaching limits
### 2. Custom Monitoring
```typescript
// Add this to your backend to monitor usage
const checkUsage = async () => {
const { data: messages } = await supabase
.from('chat_messages')
.select('id', { count: 'exact' })
const { data: users } = await supabase
.from('profiles')
.select('id', { count: 'exact' })
console.log(`Database usage: ${messages?.length || 0} messages, ${users?.length || 0} users`)
}
```
## 🚀 Deployment with Supabase
### 1. Frontend (Vercel)
```bash
# Add environment variables in Vercel
VITE_SUPABASE_URL=https://your-project.supabase.co
VITE_SUPABASE_ANON_KEY=your-anon-key
```
### 2. Backend (Hugging Face Spaces)
```bash
# Add environment variables in HF Spaces
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key
```
## 💡 Pro Tips for Free Tier
### 1. Database Optimization
- Use indexes on frequently queried columns
- Implement soft deletes instead of hard deletes
- Use JSONB for flexible data (context, settings)
### 2. Real-time Optimization
- Subscribe only to necessary channels
- Unsubscribe when components unmount
- Use filters to reduce data transfer
### 3. Authentication Optimization
- Implement proper session management
- Use refresh tokens efficiently
- Monitor active sessions
### 4. Storage Optimization
- Compress images before upload
- Set appropriate file size limits
- Use CDN for static assets
## 🔄 Migration from SQLite
### 1. Data Migration Script
```typescript
// Run this once to migrate existing data
const migrateToSupabase = async () => {
// Export from SQLite
const sqliteData = await exportFromSQLite()
// Import to Supabase
for (const table of ['profiles', 'tenants', 'chat_messages']) {
await supabase
.from(table)
.insert(sqliteData[table])
}
}
```
### 2. Gradual Migration
- Start with new features using Supabase
- Migrate existing data gradually
- Keep both systems running during transition
## 🎯 Cost-Effective Scaling
### When to Upgrade
- **Database**: >400MB storage
- **Bandwidth**: >1.5GB/month
- **Auth**: >40,000 MAU
- **Storage**: >800MB
### Upgrade Options
- **Pro Plan**: $25/month (unlimited everything)
- **Team Plan**: $599/month (enterprise features)
## 🎉 Benefits You Get
### 1. Real-time Features (Game Changer!)
- Live chat updates
- User presence indicators
- Real-time notifications
- Live collaboration
### 2. Better Authentication
- OAuth providers (Google, GitHub, etc.)
- JWT token management
- Row Level Security
- User management dashboard
### 3. Scalability
- PostgreSQL performance
- Automatic backups
- Built-in CDN
- Edge functions
### 4. Development Experience
- Auto-generated TypeScript types
- Built-in API documentation
- Database migrations
- Real-time subscriptions
---
**🎯 Bottom Line**: With Supabase's free tier, you get enterprise-grade features for $0, perfect for your MCP Chat Support System!