# networked-aframe/naf-janus-adapter architecture

> Architecture diagram of the GitHub repository networked-aframe/naf-janus-adapter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/networked-aframe/naf-janus-adapter
- Repository: https://github.com/networked-aframe/naf-janus-adapter

## Overview

##### Project type and purpose
- This is a front-end adapter library (an NPM package) for “networked-aframe,” enabling multi-user VR scenes by routing WebRTC media and data through a Janus SFU backend.
- It is not a standalone web application or server; instead it plugs into any A-Frame app that uses networked-aframe, replacing the default signaling/data layer with Janus.

##### File structure
- src/index.js: the adapter’s entry point.
- webpack.common.js / webpack.dev.js / webpack.prod.js: build pipelines producing the browser-ready bundle in dist/naf-janus-adapter.js.
- dist/: prebuilt UMD bundle for CDN/browser use.
- examples/: static HTML/JS/CSS demonstrating usage, including audio and video.
- docs/: deployment- and security-related markdown for Janus SFU.
- package.json: declares dependencies (networked-aframe, webrtc-adapter, etc.) and scripts (start, build, release).

##### README for additional insights
- Describes the adapter’s role: it registers itself on the NAF global, overrides connect(), getMediaStream(), syncOccupants(), etc.
- Outlines required external scripts: aframe.js, networked-aframe.js, webrtc-adapter shim.
- Mentions the two main flows: signaling (via Janus Data WebSockets) and media transport (WebRTC peer connections to the SFU).

##### Main components
a. Client-side components:
– A-Frame scene + networked-scene component
– networked-aframe core library (NAF)
– naf-janus-adapter plugin
– webrtc-adapter shim
b. External systems:
– Janus WebRTC Server (SFU)
– STUN/TURN servers (if configured)
c. Build pipeline (optional in high-level view):
– src/index.js → webpack → dist/naf-janus-adapter.js → published on npm or CDN
d. Example host environment:
– Static web server or any web host serving examples/index.html

##### Relationships and interactions
- On page load:
[A-Frame.js]   → provides scene
[networked-aframe.js] → registers NAF global
[webrtc-adapter.js] → normalizes WebRTC APIs
[naf-janus-adapter.js] → registers adapter on NAF.connection.adapter
- When the scene’s networked-scene.connect() is invoked:
→ Adapter opens a WebSocket/WSS connection to Janus (serverURL)
→ Attaches to janus.plugin.videoroom plugin, joins a room
→ Exchanges SDP offers/answers and ICE candidates via Janus signaling channel
→ Once peer connections for desired occupants are established, the connect() Promise resolves
- Media flow:
Local microphone/camera stream → RTCPeerConnection → Janus SFU → SFU forwards streams to other participants → adapter delivers remote MediaStream tracks to NAF

##### Architectural patterns and design principles
- Adapter pattern: wraps Janus connection logic to conform to networked-aframe’s adapter API
- Plugin architecture: loose coupling—any WebRTC-capable back end can be used if it implements the same adapter methods (setClientId, setLocalMediaStream, connect, getMediaStream, syncOccupants, enableMicrophone, etc.)
- Promise-based asynchronous initialization for stronger flow control in client apps
- Separation of concerns:
– Build/config (webpack)
– Documentation and examples (docs/, examples/)
– Core logic (src/index.js)

##### Key details to capture
- The ordering of script includes in the HTML (webrtc-adapter → aframe → networked-aframe → naf-janus-adapter)
- The default serverURL resolution logic (`/janus` → `'wss://' + location.host + '/janus'`) and override option
- The distinction between data/signaling channel and media channel
- The optional consumption of video streams via `getMediaStream(clientId, "video")`

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Browser Environment
    subgraph "Client (Browser)"
        direction TB
        WA["webrtc-adapter.js"]:::frontend
        AF["aframe.js"]:::frontend
        NAF["networked-aframe.js"]:::frontend
        ADP["naf-janus-adapter.js"]:::frontend
        WA --> AF --> NAF --> ADP
        ADP -->|"registers adapter on NAF.connection.adapter"| NAF
        click ADP "https://github.com/networked-aframe/naf-janus-adapter/blob/master/src/index.js"

        subgraph "Adapter API Methods"
            direction TB
            CM["connect() Promise"]:::api
            SM["syncOccupants()"]:::api
            GM["getMediaStream()"]:::api
        end
        ADP --> CM
        ADP --> SM
        ADP --> GM

        CM -->|"opens WSS to Janus"| WS[/"WSS Signaling Channel"/]:::signaling
        SM --> WS
        GM --> WS

        WS --> JC["janus.plugin.videoroom"]:::orange
        JC -->|"exchange SDP & ICE"| PC[("RTCPeerConnection")]:::media
        PC --> SFU[(Janus SFU)]:::orange
        SFU --> PC
        SFU -->|"forwards streams to other participants"| ADP

        %% STUN/TURN
        ST["STUN/TURN Servers"]:::cloud
        PC --> ST
    end

    %% Build Pipeline
    subgraph "Build Pipeline" 
        direction TB
        IDX["src/index.js"]:::core
        WCOM["webpack.common.js"]:::build
        WDEV["webpack.dev.js"]:::build
        WPROD["webpack.prod.js"]:::build
        DIST["dist/naf-janus-adapter.js"]:::dist
        IDX --> WCOM --> WDEV
        WCOM --> WPROD
        WPROD --> DIST
        DIST -->|"published on npm/CDN"| CDN((CDN/NPM)):::external
    end
    click IDX "https://github.com/networked-aframe/naf-janus-adapter/blob/master/src/index.js"
    click WCOM "https://github.com/networked-aframe/naf-janus-adapter/blob/master/webpack.common.js"
    click WDEV "https://github.com/networked-aframe/naf-janus-adapter/blob/master/webpack.dev.js"
    click WPROD "https://github.com/networked-aframe/naf-janus-adapter/blob/master/webpack.prod.js"
    click DIST "https://github.com/networked-aframe/naf-janus-adapter/blob/master/dist/naf-janus-adapter.js"

    %% Example Host Environment
    subgraph "Example Host Environment"
        direction TB
        EX1["examples/index.html"]:::example
        EX2["examples/audio-with-camera.html"]:::example
        JS["examples/js/audio-system.js"]:::example
        ASSETS["examples/assets/"]:::example
        CSS["examples/css/style.css"]:::example
        EX1 --> JS
        EX1 --> ASSETS
        EX1 --> CSS
    end
    click EX1 "https://github.com/networked-aframe/naf-janus-adapter/blob/master/examples/index.html"
    click EX2 "https://github.com/networked-aframe/naf-janus-adapter/blob/master/examples/audio-with-camera.html"
    click JS "https://github.com/networked-aframe/naf-janus-adapter/blob/master/examples/js/audio-system.js"
    click ASSETS "https://github.com/networked-aframe/naf-janus-adapter/tree/master/examples/assets/"
    click CSS "https://github.com/networked-aframe/naf-janus-adapter/blob/master/examples/css/style.css"

    %% Documentation & Metadata
    subgraph "Docs & Metadata"
        direction TB
        PD["docs/janus-deployment.md"]:::docs
        SJ["docs/securing-janus-with-jwt.md"]:::docs
        PK["package.json"]:::meta
        RD["README.md"]:::meta
        LIC["LICENSE"]:::meta
        CC["CODE_OF_CONDUCT.md"]:::meta
        ESL[".eslintrc.json"]:::meta
        PRE[".prettierrc.json"]:::meta
    end
    click PD "https://github.com/networked-aframe/naf-janus-adapter/blob/master/docs/janus-deployment.md"
    click SJ "https://github.com/networked-aframe/naf-janus-adapter/blob/master/docs/securing-janus-with-jwt.md"
    click PK "https://github.com/networked-aframe/naf-janus-adapter/blob/master/package.json"
    click RD "https://github.com/networked-aframe/naf-janus-adapter/blob/master/README.md"
    click LIC "https://github.com/networked-aframe/naf-janus-adapter/tree/master/LICENSE"
    click CC "https://github.com/networked-aframe/naf-janus-adapter/blob/master/CODE_OF_CONDUCT.md"
    click ESL "https://github.com/networked-aframe/naf-janus-adapter/blob/master/.eslintrc.json"
    click PRE "https://github.com/networked-aframe/naf-janus-adapter/blob/master/.prettierrc.json"

    %% Legend
    subgraph "Legend"
        direction TB
        L1["Blue: Client-side Libraries"]:::frontend
        L2["Green Cylinder: Media/Data Streams"]:::media
        L3["Orange: Janus SFU & Plugins"]:::orange
        L4["Gray Cloud: STUN/TURN Servers"]:::cloud
        L5["Dashed Box: Build Pipeline"]:::pipeline
    end

    classDef frontend fill:#AEDFFD,stroke:#0288D1,color:#000;
    classDef api fill:#E0F7FA,stroke:#006064,color:#000,stroke-dasharray: 5 5;
    classDef signaling fill:#FFF3E0,stroke:#FB8C00,color:#000;
    classDef media fill:#C8E6C9,stroke:#2E7D32,color:#000;
    classDef orange fill:#FFE0B2,stroke:#EF6C00,color:#000;
    classDef cloud fill:#E0E0E0,stroke:#9E9E9E,color:#000;
    classDef core fill:#BBDEFB,stroke:#1976D2,color:#000;
    classDef build fill:#C5CAE9,stroke:#303F9F,color:#000;
    classDef dist fill:#B3E5FC,stroke:#0288D1,color:#000;
    classDef external fill:#ECEFF1,stroke:#607D8B,color:#000;
    classDef example fill:#FFF9C4,stroke:#FBC02D,color:#000;
    classDef docs fill:#F3E5F5,stroke:#8E24AA,color:#000;
    classDef meta fill:#F5F5F5,stroke:#9E9E9E,color:#000;
    classDef pipeline stroke-dasharray: 5 5;
```
