Skip to content

Latest commit

 

History

History
176 lines (129 loc) · 4.96 KB

File metadata and controls

176 lines (129 loc) · 4.96 KB

Bare Kit Solution - Quick Summary

TL;DR

Bitcoin-tribe successfully runs Hyperswarm in React Native using react-native-bare-kit, which provides a native worklet runtime with full Node.js compatibility, including UDP socket access.


The Problem

Standard React Native cannot run Hyperswarm because:

  • No UDP socket access (dgram module)
  • Sandboxed JavaScript runtime
  • No access to OS-level networking APIs

See: WHY_HYPERDHT_NOT_IN_REACT_NATIVE.md


The Solution

Bare Kit = Native Runtime Alongside React Native

┌────────────────────┐
│  React Native      │  ← UI and app logic
│  (Hermes/JSC)      │
└─────────┬──────────┘
          │
          │ IPC via bare-rpc
          │
┌─────────▼──────────┐
│  Bare Worklet      │  ← Hyperswarm runs here!
│  (Native Runtime)  │  ← Has UDP sockets!
│  ✅ Full Node.js   │
│  ✅ UDP/TCP        │
│  ✅ Hyperswarm     │
└────────────────────┘

How It Works

1. Write Node.js code in worklet.mjs

import Hyperswarm from 'hyperswarm';

const swarm = new Hyperswarm();
// ✅ This works! We're in a native runtime!

2. Bundle with bare-pack

bare-pack --target ios --target android --out app.bundle.mjs worklet.mjs

3. Load in React Native

import { Worklet } from 'react-native-bare-kit';
import bundle from './app.bundle.mjs';

const worklet = new Worklet();
await worklet.start('/app.bundle', bundle, [seed]);

4. Communicate via RPC

// React Native → Worklet
const request = rpc.request(SEND_MESSAGE);
request.send(JSON.stringify({ message }));

// Worklet → React Native
const req = rpc.request(ON_MESSAGE);
req.send(JSON.stringify({ message }));

Key Differences

What Standard RN With Bare Kit
UDP Sockets ❌ No ✅ Yes (in worklet)
Hyperswarm ❌ Won't work ✅ Works perfectly
Architecture Single runtime Dual runtime (RN + Bare)
Complexity Simple More complex (IPC)
Bundle Size Smaller Larger (+238 MB)
True P2P ❌ No ✅ Yes

Implementation Steps

  1. Install dependencies

    yarn add react-native-bare-kit bare-rpc hyperswarm b4a buffer
  2. Create worklet (src/p2p/worklet/hyperswarm-worklet.mjs)

    • Import Hyperswarm
    • Set up RPC with clean architecture
    • Handle connections with proper state management
  3. Create manager (src/p2p/managers/HyperswarmManager.ts)

    • Load worklet
    • Set up IPC with type-safe commands
    • Provide clean, type-safe API

For complete code, see IMPLEMENTATION_GUIDE.md

  1. Generate bundle

    bare-pack --target ios --target android --out app.bundle.mjs worklet.mjs
  2. Use in React Native

    const manager = HyperswarmManager.getInstance();
    await manager.initialize(seed);
    await manager.joinRoom(roomTopic);
    
    // Set up event listeners with cleanup
    const unsubscribe = manager.onMessageReceived((event) => {
      console.log('Message:', event.message);
    });

For complete usage examples, see IMPLEMENTATION_GUIDE.md


Bitcoin-Tribe Implementation

They use this basic pattern:

  • worklet.mjs - Hyperswarm code
  • ChatPeerManager.ts - React Native manager (basic implementation)
  • app.bundle.mjs - Generated bundle
  • bare-rpc - IPC communication

It works in production! 🎉

However, for production-quality code with better architecture, see IMPLEMENTATION_GUIDE.md which improves upon their approach with:

  • Better naming (HyperswarmManager vs ChatPeerManager)
  • Type-safe RPC commands (enums vs magic numbers)
  • Clean architecture (classes vs global state)
  • Full TypeScript types
  • Proper error handling

Resources


Verdict

Hyperswarm CAN work in React Native using Bare Kit

This enables:

  • True P2P mobile apps
  • No backend server required (for P2P)
  • Full DHT and NAT traversal
  • Native performance

The trade-off is increased complexity and bundle size, but for true P2P, it's the only solution that works without rewriting the entire Hyperswarm stack.


For complete details, see BARE_KIT_SOLUTION.md
For implementation, see IMPLEMENTATION_GUIDE.md ⭐⭐