The stream reuse feature automatically reuses existing inactive streams to avoid creating unnecessary Mux live streams, helping you stay within your Mux plan limits and maintaining the same RTMP URL/stream key for OBS.
When a user clicks "Go Live" or creates a new stream:
-
Check for Reusable Streams
- System checks if the user has any previous inactive streams
- Looks for streams that have Mux credentials already configured
- Finds the most recent inactive stream
-
Reuse or Create
- If reusable stream found:
- Resets metadata (title, description, tags, stats)
- Keeps Mux stream ID, playback ID, and stream key intact
- User can use the same OBS configuration
- If no reusable stream:
- Creates a new Mux live stream
- Generates new RTMP URL and stream key
- If reusable stream found:
When reusing a stream:
- ✅ Title and description
- ✅ Tags and category
- ✅ Thumbnail
- ✅ Viewer count, earnings, and gift stats
- ✅ Timestamps (createdAt, startedAt, endedAt)
- ✅ Mux Stream ID - Same underlying Mux stream
- ✅ Mux Playback ID - Same viewing URL
- ✅ Stream Key - Same RTMP credentials for OBS
- ✅ RTMP URL - Same server endpoint
- Reduces number of Mux live streams created
- Helps stay within Mux plan limits
- No need to clean up old streams manually
- No OBS Reconfiguration Needed
- RTMP URL stays the same
- Stream key remains unchanged
- One-time OBS setup
- Reusing is faster than creating new Mux streams
- No API calls to Mux needed
- Instant stream session ready
/app/api/stream/create/route.ts
// 1. Check for existing inactive streams
const existingStreams = await convex.query(api.streams.getUserStreams, { userId });
// 2. Find most recent reusable stream
const reusableStream = existingStreams
.filter((stream) => !stream.isLive && stream.muxStreamId && stream.muxPlaybackId)
.sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0))[0];
// 3. Reuse if found, otherwise create new
if (reusableStream) {
await convex.mutation(api.streams.resetStreamForReuse, {...});
return { streamId, muxStreamId, muxPlaybackId, reused: true };
}/convex/streams.ts
- Added
resetStreamForReusemutation - Resets all user-visible data while preserving Mux credentials
/app/stream-obs/page.tsx
const data = await response.json();
if (data.reused) {
console.log("✅ Reusing existing stream - your OBS configuration remains the same!");
}User clicks "Go Live"
↓
No existing streams
↓
Create new Mux stream
↓
Generate RTMP URL + Key
↓
User configures OBS
↓
Stream ends
User clicks "Go Live" again
↓
Found inactive stream
↓
Reuse existing Mux stream
↓
Same RTMP URL + Key
↓
OBS already configured ✅
↓
Start streaming immediately
- First stream: Configure OBS with RTMP URL and key
- End stream when done
- Next stream: Just click "Go Live" and start streaming
- No need to reconfigure OBS ever again!
- New stream:
"Creating new stream for user [userId]" - Reused stream:
"Reusing stream [streamId] for user [userId]" - Client side:
"✅ Reusing existing stream - your OBS configuration remains the same!"
{
"success": true,
"streamId": "j97...",
"muxStreamId": "abc123...",
"muxPlaybackId": "xyz789...",
"reused": false
}{
"success": true,
"streamId": "j97...",
"muxStreamId": "abc123...",
"muxPlaybackId": "xyz789...",
"reused": true
}- User streams 10 times = 10 Mux live streams created
- Could exceed plan limits quickly
- Manual cleanup required
- User streams 10 times = 1 Mux live stream created
- Stays well within plan limits
- Automatic efficient resource usage
- Multiple Inactive Streams: Uses most recent one
- No Inactive Streams: Creates new stream normally
- Stream Still Live: Doesn't reuse active streams
- Missing Mux Credentials: Creates new stream
- Different Users: Each user has their own reusable stream
Potential improvements:
- Option to manually create new stream (bypass reuse)
- Stream template system (save common configurations)
- Stream archiving (mark streams to not reuse)
- Analytics on stream reuse rate
- Auto-cleanup very old streams (6+ months)
To verify stream reuse:
- Create first stream and note RTMP URL/key
- End the stream
- Create second stream
- Verify same RTMP URL/key returned
- Check console for "Reusing stream" message
- Confirm OBS works without reconfiguration
- Stream reuse is transparent to viewers
- Playback URLs remain stable
- Chat history from previous streams is preserved in database
- Stream stats are reset for each new session