# fbsamples/canvas-streaming-example architecture

> Architecture diagram of the GitHub repository fbsamples/canvas-streaming-example, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fbsamples/canvas-streaming-example
- Repository: https://github.com/fbsamples/canvas-streaming-example

## Overview

##### Project Type and Purpose
- Type: A lightweight full-stack streaming application/proxy.
- Purpose: Capture a `<canvas>` as a video stream in the browser, send it via WebSockets to a Node.js proxy that invokes FFmpeg, and push the re-wrapped/encoded stream to Facebook Live over RTMP.

##### File Structure
- server.js + package.json – the Node.js WebSocket‐to‐RTMP proxy
- www/index.html + www/js/canvasFill.js – the client static assets
- Configuration files (.gitignore, LICENSE, etc.) are not core runtime components.

##### Extract Key Architecture from the README
- Three logical parts:
1. Browser client (canvas capture + MediaRecorder + WebSocket)
2. Proxy server (Express for static, ws for WebSocket, child_process spawning FFmpeg)
3. Facebook Live API (RTMP endpoint)
- Data transforms and protocol transitions (WebM container → FLV container, Opus → AAC if needed, VP8/VP9 → H.264 copy or transcode).

##### Main Components and Their Interactions
– HTML5 Canvas
– CanvasCaptureMediaStream → MediaRecorder API
– Encoded video blobs
– WebSocket client connection to proxy
– Express static server (serves index.html + JS)
– ws WebSocket server listens on ws://…/rtmp/{encoded-URL}
– child_process.spawn(“ffmpeg”, …)
c. FFmpeg Process (inside proxy)
– Reads raw video from STDIN
– Wraps into FLV, transcodes audio to AAC if needed
– Pushes stream to Facebook Live RTMP endpoint
– RTMP ingest endpoint (rtmp:// or rtmps://)
– CDN distribution to viewers

##### Architectural Patterns and Principles
- Streaming pipeline pattern: discrete stages (capture → encode → transport → remux/transcode → publish)
- Separation of concerns: client handles capture, server handles transport and codec compatibility
- Proxy pattern: translate WebSocket stream into RTMP
- Child-process isolation: FFmpeg in its own process for security and stability

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser Client Layer
    subgraph "Browser Client"
        direction TB
        Index["index.html"]:::frontend
        CanvasJS["canvasFill.js"]:::frontend
        MediaRecorder["MediaRecorder API"]:::frontend
        WebSocketClient["WebSocket Client"]:::frontend
    end

    %% Proxy Server Layer
    subgraph "Proxy Server"
        direction TB
        ExpressServer["Express Static Server"]:::backend
        StaticAssets["www/"]:::backend
        WSServer["ws WebSocket Server"]:::backend
        FFmpegProcess["FFmpeg Process"]:::backend
        Dependencies["package.json"]:::backend
    end

    %% External Service Layer
    subgraph "Facebook Live API"
        direction TB
        ExternalFacebook["Facebook Live RTMP Endpoint"]:::external
    end

    %% Reference Diagram
    ReferenceDiagram["Reference Architecture Diagram"]:::external

    %% Connections - Browser Client
    CanvasJS -->|"initiates capture"| MediaRecorder
    MediaRecorder -->|"encoded WebM blobs"| WebSocketClient
    WebSocketClient -->|"WebSocket"| WSServer
    Index --> CanvasJS

    %% Connections - Proxy Server
    ExpressServer -->|"serves static"| StaticAssets
    StaticAssets --> Index
    StaticAssets --> CanvasJS
    Dependencies --> ExpressServer
    Dependencies --> WSServer
    WSServer -->|"spawn ffmpeg"| FFmpegProcess
    WSServer -->|"stdin stream"| FFmpegProcess

    %% Streaming to Facebook
    FFmpegProcess -->|"RTMP (FLV/AAC)"| ExternalFacebook

    %% Reference Diagram
    ReferenceDiagram

    %% Click Events
    click Index "https://github.com/fbsamples/canvas-streaming-example/blob/master/www/index.html"
    click CanvasJS "https://github.com/fbsamples/canvas-streaming-example/blob/master/www/js/canvasFill.js"
    click ExpressServer "https://github.com/fbsamples/canvas-streaming-example/blob/master/server.js"
    click WSServer "https://github.com/fbsamples/canvas-streaming-example/blob/master/server.js"
    click FFmpegProcess "https://github.com/fbsamples/canvas-streaming-example/blob/master/server.js"
    click Dependencies "https://github.com/fbsamples/canvas-streaming-example/blob/master/package.json"
    click StaticAssets "https://github.com/fbsamples/canvas-streaming-example/tree/master/www/"
    click ReferenceDiagram "https://github.com/fbsamples/canvas-streaming-example/blob/master/doc/architecture.png"

    %% Styles
    classDef frontend fill:#FFA500,stroke:#333,stroke-width:1px
    classDef backend fill:#FFFF00,stroke:#333,stroke-width:1px
    classDef external fill:#00FF00,stroke:#333,stroke-width:1px
```
