Web / JavaScript SDK
Build peer-to-peer web applications with LiteP2P's JavaScript SDK. Works in browsers and Node.js.
Requirements
Modern browser with WebRTC support, or Node.js 16+
Installation
npm / yarn / pnpm
# npm
npm install @litep2p/sdk
# yarn
yarn add @litep2p/sdk
# pnpm
pnpm add @litep2p/sdk
CDN
<script src="https://cdn.litep2p.io/sdk/2.1.0/litep2p.min.js"></script>
<!-- Or use ES modules -->
<script type="module">
import { LiteP2P } from 'https://cdn.litep2p.io/sdk/2.1.0/litep2p.esm.js';
</script>
Quick Start
ES Modules
import { LiteP2P, PeerConfig } from '@litep2p/sdk';
// Initialize
const config = new PeerConfig({
appId: 'your-app-id'
});
const p2p = new LiteP2P(config);
// Connect to the network
await p2p.connect();
// Send a message
await p2p.send(peerId, data);
// Receive messages
p2p.onMessage((message) => {
console.log(`From ${message.peerId}:`, message.data);
});
CommonJS
const { LiteP2P, PeerConfig } = require('@litep2p/sdk');
const config = new PeerConfig({ appId: 'your-app-id' });
const p2p = new LiteP2P(config);
p2p.connect().then(() => {
console.log('Connected!');
});
Browser Support
| Browser | Minimum Version | Notes |
|---|---|---|
| Chrome | 80+ | Full support |
| Firefox | 78+ | Full support |
| Safari | 14+ | Full support |
| Edge | 80+ | Full support |
| Safari iOS | 14+ | Limited background |
Features
WebRTC Native
Uses browser WebRTC APIs for true peer-to-peer connections.
TypeScript Support
Full TypeScript definitions included for type-safe development.
Tree Shakeable
Import only what you need for smaller bundle sizes.
Framework Agnostic
Works with React, Vue, Angular, Svelte, or vanilla JS.
React Example
import { useEffect, useState } from 'react';
import { LiteP2P, PeerConfig } from '@litep2p/sdk';
function useLiteP2P(appId: string) {
const [p2p, setP2P] = useState<LiteP2P | null>(null);
const [connected, setConnected] = useState(false);
useEffect(() => {
const config = new PeerConfig({ appId });
const instance = new LiteP2P(config);
instance.connect().then(() => {
setConnected(true);
});
setP2P(instance);
return () => {
instance.disconnect();
};
}, [appId]);
return { p2p, connected };
}
// Usage
function ChatApp() {
const { p2p, connected } = useLiteP2P('your-app-id');
if (!connected) return <div>Connecting...</div>;
return <div>Connected! Peer ID: {p2p?.peerId}</div>;
}
Next Steps
- Bundler Setup – Webpack, Vite, Rollup configuration
- TypeScript – Type definitions and usage
- Framework Integration – React, Vue, Angular guides
- Node.js – Server-side usage