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.
Standard React Native cannot run Hyperswarm because:
- No UDP socket access (
dgrammodule) - Sandboxed JavaScript runtime
- No access to OS-level networking APIs
See: WHY_HYPERDHT_NOT_IN_REACT_NATIVE.md
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 │
└────────────────────┘
import Hyperswarm from 'hyperswarm';
const swarm = new Hyperswarm();
// ✅ This works! We're in a native runtime!bare-pack --target ios --target android --out app.bundle.mjs worklet.mjsimport { Worklet } from 'react-native-bare-kit';
import bundle from './app.bundle.mjs';
const worklet = new Worklet();
await worklet.start('/app.bundle', bundle, [seed]);// 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 }));| 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 |
-
Install dependencies
yarn add react-native-bare-kit bare-rpc hyperswarm b4a buffer
-
Create worklet (
src/p2p/worklet/hyperswarm-worklet.mjs)- Import Hyperswarm
- Set up RPC with clean architecture
- Handle connections with proper state management
-
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
-
Generate bundle
bare-pack --target ios --target android --out app.bundle.mjs worklet.mjs
-
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
They use this basic pattern:
worklet.mjs- Hyperswarm codeChatPeerManager.ts- React Native manager (basic implementation)app.bundle.mjs- Generated bundlebare-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 (
HyperswarmManagervsChatPeerManager) - Type-safe RPC commands (enums vs magic numbers)
- Clean architecture (classes vs global state)
- Full TypeScript types
- Proper error handling
- Full Explanation: BARE_KIT_SOLUTION.md
- Implementation Guide: IMPLEMENTATION_GUIDE.md ⭐⭐
- Reference Code: bitcoin-tribe/src/services/p2p
- Bare Kit Docs: github.com/holepunchto/bare-kit
✅ 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 ⭐⭐