@minip2p/react-native
v0.11.9
Published
React Native bindings for minip2p
Maintainers
Readme
@minip2p/react-native
The React Native package for minip2p.
The package is pre-1.0 and may introduce breaking changes between releases.
Requirements
- React Native 0.87 with the New Architecture and Hermes
- iOS 15.1 or newer
- Android API 24 or newer
Expo Go cannot load minip2p. Expo projects must use a development build.
Installation
pnpm add @minip2p/react-nativeRebuild the native app after installing the package.
Usage
import { Minip2p, generateSecretKey } from "@minip2p/react-native";
const endpoint = Minip2p.create({
secretKey: generateSecretKey(),
mdns: true,
protocols: ["/example/files/1"],
});
const unsubscribe = endpoint.on("peerReady", ({ peerId }) => {
console.log("peer ready", peerId);
});
endpoint.subscribe("/example/chat/1");
endpoint.publish("/example/chat/1", "hello");
const path = await endpoint.connect(remoteAddress);
// Optional: wait for Identify when you need advertised protocols.
await endpoint.waitPeerReady(path.peerId);
const rttMs = await endpoint.ping(path.peerId);
const stream = await endpoint.openStream(path.peerId, "/example/files/1");
await stream.write(new Uint8Array([1, 2, 3]));
stream.closeWrite();
unsubscribe();
endpoint.close();Minip2p.create starts an endpoint. useMinip2p manages one for a React component, while bindAppState keeps its active state synchronized with the application.
Configuration defaults to gossipsub with signed messages and no relay or discovery. Set mdns: true for local discovery and ensure the host app has local-network and multicast permissions.
Important behavior
- Operations support timeouts and
AbortSignalcancellation. - Events are delivered FIFO through a bounded queue; handle
queueOverflowby refreshing queryable state. - Use a named
streamhandler to claim inbound streams. - A peer holds one connection:
connectionReplacedmoves it to a newer connection and ends streams on the old one, andwaitPeerReadywaits for the current connection'speerReady. - Unsigned pubsub payloads and peer IDs are attacker-controlled.
close()rejects pending work, closes streams, and shuts down the endpoint.
License
MIT
