Skip to content

Latest commit

ย 

History

History
397 lines (303 loc) ยท 9.14 KB

File metadata and controls

397 lines (303 loc) ยท 9.14 KB

React Native Playground with Live Streaming

A complete React Native playground that allows you to stream your mobile app's view in real-time to a web browser using WebSocket and ffmpeg.

๐ŸŽฏ Overview

This project consists of three main components:

  1. Backend Server - Node.js WebSocket server with ffmpeg streaming
  2. Expo Mobile App - React Native app that captures and streams views
  3. Next.js Frontend - Web interface to view the live stream

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         WebSocket          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Expo Mobile   โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€ (Base64 PNG) โ”€โ”€โ”€โ”€> โ”‚  Backend Server  โ”‚
โ”‚      App        โ”‚                             โ”‚  (Node.js + WS)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                         โ”‚
                                                    ffmpeg pipe
                                                         โ”‚
                                                         โ–ผ
                                                  MJPEG Stream
                                                         โ”‚
                                                         โ–ผ
                                                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                                โ”‚  Next.js Web UI  โ”‚
                                                โ”‚  (Stream Viewer) โ”‚
                                                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“‹ Prerequisites

  • Node.js 18+ and npm
  • ffmpeg installed and available in PATH
    # macOS
    brew install ffmpeg
    
    # Ubuntu/Debian
    sudo apt-get install ffmpeg
    
    # Windows
    # Download from https://ffmpeg.org/download.html
  • Expo CLI (optional, for easier development)
    npm install -g expo-cli
  • iOS Simulator or Android Emulator or Physical Device

๐Ÿš€ Quick Start

1. Backend Server Setup

cd react_native_playground_backend

# Install dependencies
npm install

# Start the server
npm run dev

The server will start on:

  • HTTP: http://localhost:3000
  • WebSocket: ws://localhost:3001
  • MJPEG Stream: http://localhost:3000/stream.mjpeg

2. Expo Mobile App Setup

cd expo-stream-app

# Install dependencies
npm install

# Start Expo
npx expo start

Important: Update the server IP in app/index.tsx:

const [serverIP, setServerIP] = useState("YOUR_COMPUTER_IP"); // e.g., "192.168.1.100"

To find your IP:

# macOS/Linux
ifconfig | grep "inet "

# Windows
ipconfig

Then:

  1. Scan QR code with Expo Go app (iOS/Android)
  2. Or press i for iOS simulator / a for Android emulator
  3. Enter your server IP in the app
  4. Press "Start Streaming"

3. Frontend Web UI Setup

cd react-native-playground-frontend

# Install dependencies
npm install

# Start development server
npm run dev

Open http://localhost:3000 in your browser to see the live stream!

๐ŸŽฎ Usage

Starting a Stream

  1. Start Backend Server

    cd react_native_playground_backend
    npm run dev
  2. Start Mobile App

    cd expo-stream-app
    npx expo start
    • Open in Expo Go or simulator
    • Enter your computer's IP address
    • Tap "Start Streaming"
  3. View Stream in Browser

    cd react-native-playground-frontend
    npm run dev

Alternative: View Stream Directly

You can also view the MJPEG stream directly:

In VLC:

  • Media โ†’ Open Network Stream
  • Enter: http://YOUR_IP:3000/stream.mjpeg

In Browser:

  • Navigate to: http://YOUR_IP:3000/stream.mjpeg
  • (Some browsers support MJPEG natively)

๐Ÿ“ Project Structure

native_playground/
โ”œโ”€โ”€ react_native_playground_backend/
โ”‚   โ”œโ”€โ”€ server.ts                 # WebSocket + ffmpeg server
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ tsconfig.json
โ”‚
โ”œโ”€โ”€ expo-stream-app/
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ _layout.tsx          # App layout
โ”‚   โ”‚   โ””โ”€โ”€ index.tsx            # Main streaming screen
โ”‚   โ”œโ”€โ”€ app.json
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ tsconfig.json
โ”‚
โ””โ”€โ”€ react-native-playground-frontend/
    โ”œโ”€โ”€ src/
    โ”‚   โ”œโ”€โ”€ app/
    โ”‚   โ”‚   โ””โ”€โ”€ page.tsx
    โ”‚   โ””โ”€โ”€ components/
    โ”‚       โ”œโ”€โ”€ PlaygroundLayout.tsx
    โ”‚       โ”œโ”€โ”€ StreamViewer.tsx  # Stream display component
    โ”‚       โ””โ”€โ”€ ...
    โ”œโ”€โ”€ package.json
    โ””โ”€โ”€ next.config.ts

๐Ÿ”ง Configuration

Backend Server

Edit react_native_playground_backend/server.ts:

const HTTP_PORT = 3000;  // HTTP server port
const WS_PORT = 3001;    // WebSocket port

// ffmpeg settings
"-r", "10",              // Frame rate (FPS)
"-vf", "scale=640:-2",   // Video scale
"-q:v", "5",             // JPEG quality (2-31, lower = better)

Mobile App

Edit expo-stream-app/app/index.tsx:

const intervalMs = 100;  // Capture interval (100ms = 10 FPS)

// Capture settings
format: "png",
quality: 0.7,            // Image quality (0-1)
result: "base64",

Frontend

Edit react-native-playground-frontend/src/components/PlaygroundLayout.tsx:

<StreamViewer 
  serverUrl="http://localhost:3000"  // Backend URL
  theme={currentSettings.theme}
/>

๐Ÿ› ๏ธ Tech Stack

Backend

  • Node.js + TypeScript
  • Express.js - HTTP server
  • ws - WebSocket server
  • ffmpeg - Video encoding
  • CORS - Cross-origin support

Mobile App

  • Expo ~52.0.0
  • React Native 0.76.5
  • react-native-view-shot - View capture
  • expo-router - Navigation

Frontend

  • Next.js 15.5.3
  • React 19.1.0
  • Monaco Editor - Code editor
  • TailwindCSS - Styling
  • Lucide React - Icons

๐ŸŽจ Features

Backend

  • โœ… WebSocket server for receiving frames
  • โœ… ffmpeg integration for MJPEG encoding
  • โœ… HTTP endpoint for stream delivery
  • โœ… Status monitoring endpoints
  • โœ… Graceful shutdown handling

Mobile App

  • โœ… Real-time view capture at 10 FPS
  • โœ… WebSocket streaming
  • โœ… Configurable server IP
  • โœ… Beautiful UI with live preview
  • โœ… Connection status indicators
  • โœ… Works in Expo Go (no native build needed)

Frontend

  • โœ… Live MJPEG stream display
  • โœ… Device frame UI
  • โœ… Connection status monitoring
  • โœ… Auto-refresh capability
  • โœ… Dark/Light theme support
  • โœ… Responsive layout

๐Ÿ› Troubleshooting

Stream Not Appearing

  1. Check ffmpeg installation:

    ffmpeg -version
  2. Verify server is running:

    curl http://localhost:3000/status
  3. Check mobile app connection:

    • Ensure mobile device and computer are on same network
    • Verify IP address is correct
    • Check firewall settings
  4. Test stream directly:

    curl http://localhost:3000/stream.mjpeg

WebSocket Connection Failed

  • Ensure backend server is running on port 3001
  • Check that mobile device can reach server IP
  • Verify no firewall blocking WebSocket connections

Low Frame Rate

  • Reduce capture quality in mobile app
  • Decrease image resolution in ffmpeg settings
  • Check network bandwidth

๐Ÿš€ Performance Optimization

For Better Quality

// Mobile app - increase quality
quality: 0.9,

// Backend - better JPEG quality
"-q:v", "2",

For Better Performance

// Mobile app - lower quality
quality: 0.5,

// Backend - smaller resolution
"-vf", "scale=480:-2",

// Reduce frame rate
"-r", "5",

๐Ÿ“ API Endpoints

Backend Server

Endpoint Method Description
/ GET Health check
/status GET Server status (ffmpeg, clients)
/stream.mjpeg GET MJPEG video stream
ws://localhost:3001 WebSocket Frame receiver

WebSocket Messages

From Mobile App:

{
  "type": "frame",
  "data": "<base64-encoded-png>"
}
{
  "type": "stop"
}

๐ŸŽฏ Use Cases

  • Live Coding Demos - Show React Native development in real-time
  • Remote Debugging - Share app state with team members
  • Presentations - Display mobile app on larger screens
  • Testing - Monitor app behavior during automated tests
  • Education - Teaching React Native development

๐Ÿ”ฎ Future Enhancements

  • Binary frame transmission (reduce overhead)
  • HLS/RTMP output for CDN streaming
  • Multiple device support
  • Recording capability
  • Audio streaming
  • Touch event overlay
  • Performance metrics display

๐Ÿ“„ License

ISC

๐Ÿ‘ฅ Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Open a Pull Request

๐Ÿ™ Acknowledgments

  • Built with inspiration from the React Native community
  • Uses ffmpeg for efficient video encoding
  • Powered by Expo for seamless mobile development

Happy Streaming! ๐ŸŽฅ