AGENT SKILL

ams-webrtc-pro

暂无简介

License 许可
MIT
所属仓库
TanNhatCMS/jarvis
最近更新
2026-11-07
Stars
★ 0
Forks
1

03 SKILL.md 正文

WebRTC Pro

A practical guide to WebRTC: browser-to-browser real-time audio, video, and data — signaling, peer connections, NAT traversal (STUN/TURN), media handling, and the infrastructure decisions that determine whether calls actually connect.

Overview

WebRTC enables peer-to-peer real-time communication in the browser: audio/video calls, screen sharing, and data channels — with encryption (DTLS-SRTP) built in. The catch: browsers can't find each other alone. You need signaling (your server exchanging session descriptions/ICE candidates) and NAT traversal (STUN to discover addresses, TURN to relay when direct fails). Media flows peer-to-peer; signaling and TURN are your infrastructure.

When to use

  • 1:1 or group video/voice calls.
  • Screen sharing.
  • Peer-to-peer data transfer (files, game state) via data channels.
  • Live broadcasting with sub-second latency (WHIP/WHEP or custom).

Core concepts

  • Signaling. Your WebSocket/HTTP channel exchanging: offers/answers (SDP session descriptions) and ICE candidates. Not standardized — you design it. Keep it simple: { type: 'offer'|'answer'|'candidate', ... }.
  • RTCPeerConnection. The core object: addTrack/createOffer → set local/remote descriptions → ICE gathering → connected. One per peer (mesh) or per SFU uplink.
  • ICE. Interactive Connectivity Establishment: tries host → STUN (server-reflexive) → TURN (relay) candidates, picks the best working path. Most failures are ICE failures.
  • STUN. Discovers your public address (stun:stun.l.google.com:19302 or your own). Cheap, stateless.
  • TURN. Relays media when direct connection fails (symmetric NATs, restrictive firewalls). You must run TURN — without it, 10–30% of calls fail. Bandwidth costs money; budget it.
  • Media devices. getUserMedia({ video, audio }) — permission prompt; handle denial gracefully. getDisplayMedia for screen share. Device selection via enumerateDevices.
  • Data channels. createDataChannel — low-latency peer-to-peer messaging (chat in calls, file transfer, game state). Ordered/reliable or unordered/unreliable modes.
  • Topologies. Mesh (each peer connects to each — fine for ≤4), SFU (Selective Forwarding Unit server routes streams — the standard for groups), MCU (server mixes — heavy, legacy).

Practical workflow

1. Signaling flow (1:1).

A: pc.createOffer() → setLocalDescription → send offer via signaling
B: receive offer → setRemoteDescription → createAnswer → setLocalDescription → send answer
A: receive answer → setRemoteDescription
Both: exchange ICE candidates as they trickle (onicecandidate → signaling → addIceCandidate)

2. Basic setup.

const pc = new RTCPeerConnection({ iceServers: [
  { urls: 'stun:stun.example.com:3478' },
  { urls: 'turn:turn.example.com:3478', username: 'user', credential: 'pass' },
]});
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
stream.getTracks().forEach(t => pc.addTrack(t, stream));
pc.ontrack = (e) => { remoteVideo.srcObject = e.streams[0]; };

3. TURN. Deploy coturn (or managed TURN). Generate time-limited credentials server-side (don't hardcode). Test with TURN-only (block UDP/host candidates) to prove relay works.

4. Call UX. Pre-call device check (camera/mic preview + selection); in-call: mute, camera toggle, screen share, connection quality indicator; handle iceconnectionstatechange → "reconnecting..." UI → recovery or clean failure.

5. Group calls. Don't mesh beyond ~4. Use an SFU (LiveKit, Janus, mediasoup, or managed) — clients publish once, SFU forwards selectively.

Common pitfalls

  • No TURN server. "Works on my network" then fails for users behind corporate/symmetric NATs. TURN is mandatory infrastructure, not optional.
  • Hardcoded TURN credentials. Long-lived credentials in client code get abused (bandwidth theft). Time-limited credentials minted server-side.
  • Mesh for groups. 8-person mesh = 56 connections, everyone's uplink saturated. SFU for groups.
  • Ignoring ICE failures. iceConnectionState === 'failed' needs restart (pc.restartIce()) or re-offer — not a silent dead call.
  • getUserMedia without fallback. Permission denied / no devices = crash without handling. Graceful degradation (audio-only, or join muted).
  • Echo. Open speakers + mic = feedback. Advise headsets; use echoCancellation: true; the browser helps but physics wins.
  • Signaling as an afterthought. Dropped signaling messages = stuck calls. Reliable transport (WebSocket with reconnection) + message acks for critical signals.
  • No stats monitoring. pc.getStats() reveals bitrate, packet loss, jitter — without it you're blind to quality issues. Monitor and adapt (degrade video before dropping audio).
  • Bandwidth blindness. HD video × N participants costs real TURN/server bandwidth. Budget per concurrent user; consider simulcast (SFU sends appropriate quality per receiver).
  • Mobile battery/heat. Sustained WebRTC on phones drains fast. Prefer audio-first defaults on mobile; pause video when backgrounded.

04 同仓库更多 Skills

TanNhatCMS
TanNhatCMS/jarvis

web-scrape

Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

design-taste-frontend

tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

test-driven-development

Use when implementing any feature or bugfix, before writing implementation code

★ 0
TanNhatCMS
TanNhatCMS/jarvis

systematic-debugging

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes

★ 0
TanNhatCMS
TanNhatCMS/jarvis

requesting-code-review

Use when completing tasks, implementing major features, or before merging to verify work meets requirements

★ 0
TanNhatCMS
TanNhatCMS/jarvis

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

★ 0