# Troubleshooting Guide

## Installation Issues

### Error: "Cannot find module"
```bash
rm -rf node_modules package-lock.json
npm install
```

### Error: "Port 3000 already in use"
```bash
# Find and kill the process
lsof -ti:3000 | xargs kill -9

# Or change port in server/index.js
```

### Error: "Port 5173 already in use"
```bash
# Kill the process
lsof -ti:5173 | xargs kill -9

# Or change port in vite.config.js
```

## Runtime Issues

### Camera/Microphone Access Denied

**Symptoms**: "Permission denied" or camera not starting

**Solutions**:
1. Check browser permissions (click lock icon in address bar)
2. Ensure you're on localhost or HTTPS
3. Close other apps using camera (Zoom, Skype, etc.)
4. Try different browser
5. Restart browser completely

### No Video/Audio from Remote Participants

**Symptoms**: Can see yourself but not others

**Solutions**:
1. Click anywhere on the page (autoplay policy)
2. Check browser console for errors
3. Verify other participants have camera/mic enabled
4. Check firewall settings
5. Ensure WebRTC ports (40000-49999) are open

### Black Screen or Frozen Video

**Symptoms**: Video shows but is black or frozen

**Solutions**:
1. Refresh the page
2. Check camera is working in other apps
3. Try different video resolution
4. Check CPU usage (close other apps)
5. Update browser to latest version

### Audio Echo or Feedback

**Symptoms**: Hearing echo or feedback noise

**Solutions**:
1. Use headphones
2. Ensure local video is muted
3. Check multiple tabs aren't open
4. Reduce speaker volume
5. Enable echo cancellation in browser settings

### Connection Drops Frequently

**Symptoms**: Participants keep disconnecting

**Solutions**:
1. Check internet connection stability
2. Reduce number of participants
3. Close bandwidth-heavy apps
4. Use wired connection instead of WiFi
5. Check server resources (CPU, memory)

## Server Issues

### Server Won't Start

**Symptoms**: Error when running `npm run server`

**Solutions**:
```bash
# Check Node.js version (should be 16+)
node --version

# Reinstall dependencies
npm install

# Check for syntax errors
npm run server 2>&1 | grep -i error
```

### Mediasoup Worker Died

**Symptoms**: "mediasoup worker died" in console

**Solutions**:
1. Restart server
2. Check system resources
3. Verify ports 40000-49999 are available
4. Check server logs for details
5. Ensure proper permissions

### Socket.IO Connection Failed

**Symptoms**: "Connection failed" or "Transport error"

**Solutions**:
1. Verify server is running
2. Check CORS settings
3. Ensure correct URL in client.js
4. Check firewall/antivirus
5. Try different network

## Client Issues

### Vite Dev Server Won't Start

**Symptoms**: Error when running `npm run client`

**Solutions**:
```bash
# Clear Vite cache
rm -rf node_modules/.vite

# Reinstall
npm install

# Try different port
# Edit vite.config.js and change port
```

### Module Import Errors

**Symptoms**: "Failed to resolve module" errors

**Solutions**:
1. Check internet connection (using CDN imports)
2. Clear browser cache
3. Try different browser
4. Check browser console for specific module
5. Verify CDN URLs are accessible

### UI Not Loading

**Symptoms**: Blank page or broken layout

**Solutions**:
1. Hard refresh: Ctrl+Shift+R (Cmd+Shift+R on Mac)
2. Clear browser cache
3. Check browser console for errors
4. Verify index.html loaded correctly
5. Try incognito/private mode

## Network Issues

### Can't Connect from Different Computer

**Symptoms**: Works on localhost but not from other devices

**Solutions**:
1. Find your local IP:
   ```bash
   # Mac/Linux
   ifconfig | grep "inet "
   
   # Windows
   ipconfig
   ```

2. Update client.js:
   ```javascript
   // Change this line (around line 48)
   socket = io("http://YOUR_LOCAL_IP:3000");
   ```

3. Ensure firewall allows connections:
   ```bash
   # Mac
   System Preferences → Security & Privacy → Firewall
   
   # Windows
   Control Panel → Windows Defender Firewall
   ```

4. Check router settings (port forwarding if needed)

### Works on LAN but not Internet

**Symptoms**: Works locally but not over internet

**Solutions**:
1. Configure TURN server (required for NAT traversal)
2. Use ngrok for testing:
   ```bash
   ngrok http 3000
   ```
3. Deploy to cloud service
4. Configure proper STUN/TURN servers
5. Set up port forwarding on router

## Performance Issues

### High CPU Usage

**Symptoms**: Computer slows down during call

**Solutions**:
1. Reduce video quality
2. Limit number of participants
3. Close other applications
4. Disable screen sharing
5. Use hardware acceleration in browser

### Laggy Video/Audio

**Symptoms**: Choppy or delayed video/audio

**Solutions**:
1. Check internet speed (need 2-3 Mbps per participant)
2. Close bandwidth-heavy apps
3. Use wired connection
4. Reduce video resolution
5. Limit participants

### High Memory Usage

**Symptoms**: Browser uses too much RAM

**Solutions**:
1. Close unused tabs
2. Restart browser
3. Update browser
4. Reduce video quality
5. Use Chrome/Edge (better WebRTC support)

## Browser-Specific Issues

### Safari Issues
- Screen sharing may not work (Safari limitation)
- Use Chrome/Firefox for full features
- Ensure Safari is updated to latest version

### Firefox Issues
- May need to enable media.peerconnection.enabled
- Check about:config settings
- Update to latest version

### Mobile Browser Issues
- Limited support on mobile browsers
- Use native apps for better experience
- Chrome mobile has best support

## Debugging Tips

### Enable Verbose Logging

**Client Side** (browser console):
```javascript
localStorage.debug = '*';
```

**Server Side** (add to server/index.js):
```javascript
process.env.DEBUG = 'mediasoup*';
```

### Check WebRTC Stats

In browser console:
```javascript
// Get peer connection stats
const pc = RTCPeerConnection.prototype;
console.log(pc);
```

### Monitor Network Traffic

1. Open browser DevTools (F12)
2. Go to Network tab
3. Filter by WS (WebSocket)
4. Monitor Socket.IO messages

### Check Server Logs

```bash
# Run server with verbose output
DEBUG=* npm run server

# Or check specific module
DEBUG=mediasoup* npm run server
```

## Still Having Issues?

1. **Check Console Logs**:
   - Browser: F12 → Console
   - Server: Terminal output

2. **Verify Setup**:
   ```bash
   node --version  # Should be 16+
   npm --version   # Should be 8+
   ```

3. **Test Basic Functionality**:
   - Can you access http://localhost:5173?
   - Can you access http://localhost:3000?
   - Do you see Socket.IO connection in Network tab?

4. **Common Error Messages**:
   - "Cannot consume": RTP capabilities mismatch
   - "Transport error": Network/firewall issue
   - "Producer closed": User disconnected
   - "Worker died": Server resource issue

5. **Reset Everything**:
   ```bash
   # Kill all processes
   pkill -f node
   
   # Clean install
   rm -rf node_modules package-lock.json
   npm install
   
   # Restart
   npm run dev
   ```

## Getting Help

When reporting issues, include:
- Operating system
- Browser and version
- Node.js version
- Error messages from console
- Steps to reproduce
- Server logs

---

**Most issues are solved by refreshing the page or restarting the server!** 🔄
