Symptom: You add a magnet URI or torrent file, but no peers are discovered and downloading doesn’t start.Solution: The torrent must be seeded by a WebRTC-capable client. WebTorrent in the browser can only connect to other WebRTC peers.Make sure the torrent is being seeded by:
torrent.on('ready', () => { // Wait until 5% is downloaded const interval = setInterval(() => { if (torrent.progress > 0.05) { clearInterval(interval) file.streamTo(video) video.play() } }, 1000)})
Increase max connections:
const client = new WebTorrent({ maxConns: 100 // Default is 55})
Problem: Video seeking doesn't work
Symptom: Clicking on the progress bar doesn’t jump to that position.Solution: Only MP4 files have full seeking support. For other formats, seeking may be limited.If using MP4 and seeking still doesn’t work:
// Ensure service worker is properly set upconst controller = await navigator.serviceWorker.register('./sw.min.js', { scope: './' // Must match the scope where your page is served})await navigator.serviceWorker.ready// Verify the server is createdconst server = client.createServer({ controller })console.log('Server created:', server)
Symptom:navigator.serviceWorker.register() throws an error.Common causes:
HTTPS required: Service Workers only work on HTTPS (or localhost)
Scope mismatch: Service Worker file must be at or above the scope path
File not found:sw.min.js is not being served correctly
Solutions:
Use HTTPS or localhost:
# For development, use localhostnpx serve -l 3000# Access via http://localhost:3000
Check file path:
// sw.min.js must be in the root directory or above your HTML fileconst controller = await navigator.serviceWorker.register('./sw.min.js', { scope: './' // Scope must be at or below sw.min.js location})
Copy service worker file:
# Copy from node_modulescp node_modules/webtorrent/sw.min.js public/sw.min.js
Problem: Service Worker installed but streaming doesn't work
Check service worker is active:
const registration = await navigator.serviceWorker.register('./sw.min.js')console.log('Registration:', registration)await navigator.serviceWorker.readyconsole.log('Service Worker is ready')const server = client.createServer({ controller: registration })console.log('Server created:', server)
Debug in DevTools:
Open Chrome DevTools → Application → Service Workers
import WebTorrent from 'webtorrent'if (!WebTorrent.WEBRTC_SUPPORT) { console.error('WebRTC is not supported in this browser')} else { console.log('WebRTC is supported!')}// Check MediaSource supportif ('MediaSource' in window) { console.log('MediaSource API is supported')} else { console.warn('MediaSource API is not supported - streaming may not work')}// Check Service Worker supportif ('serviceWorker' in navigator) { console.log('Service Worker is supported')} else { console.warn('Service Worker is not supported')}