Code Examples
Learn by example! These working examples demonstrate various features of the Vaultys Peer SDK.
π Available Examplesβ
Basic Usageβ
File: examples/basic-usage.ts
A simple example showing the fundamental concepts:
- Initializing the SDK
- Connecting to peers
- Sending and receiving messages
- Basic event handling
# Run the example
npm run example
Connect Peersβ
File: examples/connectPeers.ts
Demonstrates peer connection management:
- Connecting two peers
- Managing multiple connections
- Connection state handling
- Automatic reconnection
# Run the example
npm run example:2peers
Talk - CLI Chat Applicationβ
File: examples/talk.ts
A complete command-line chat application featuring:
- Interactive CLI interface
- Real-time messaging
- File sharing
- Contact management
- Message history
- Status indicators
# Run the example
npm run example:talk
π― Quick Start Examplesβ
1. Send Your First Messageβ
import { setupVaultysPeerSDK } from '@vaultys/peer-sdk';
const peer = setupVaultysPeerSDK();
await peer.initialize('did:vaultys:alice');
const bob = await peer.addContact('did:vaultys:bob');
await peer.sendMessage(bob.did, 'Hello Bob!');
2. Listen for Messagesβ
peer.on('message-received', (message) => {
console.log(`${message.from}: ${message.content}`);
});
peer.on('contact-online', (contact) => {
console.log(`${contact.nickname || contact.did} is online`);
});
3. Share Filesβ
// Send a file
const fileBuffer = await fs.readFile('./document.pdf');
await peer.sendMessage(bob.did, 'Sending document', 'file', {
name: 'document.pdf',
size: fileBuffer.length,
type: 'application/pdf',
data: fileBuffer
});
// Receive files
peer.on('message-received', (message) => {
if (message.type === 'file') {
await fs.writeFile(
`./${message.fileData.name}`,
message.fileData.data
);
console.log('File saved:', message.fileData.name);
}
});
4. Lazy Loading with Multiple Peersβ
// Add thousands of contacts with zero overhead
const contacts = await peer.bulkAddContacts([
'did:vaultys:user1',
'did:vaultys:user2',
// ... thousands more
]);
// Connection only established when needed
await peer.sendMessage('did:vaultys:user500', 'Hello!');
// NOW the connection to user500 is established
5. Custom Signaling Serverβ
const peer = setupVaultysPeerSDK({
relay: {
host: 'your-signal-server.com',
port: 443,
path: '/myapp',
secure: true,
config: {
iceServers: [
{ urls: 'stun:stun.example.com:3478' },
{
urls: 'turn:turn.example.com:3478',
username: 'user',
credential: 'pass'
}
]
}
}
});
π Example Categoriesβ
Messaging Examplesβ
- Text messaging
- File sharing
- Group messaging
- Message persistence
- Offline message queueing
Connection Examplesβ
- Peer discovery
- Connection management
- Auto-reconnection
- Health monitoring
- Connection pooling
Storage Examplesβ
- Memory storage
- IndexedDB storage
- File system storage
- Custom storage providers
Advanced Examplesβ
- Custom protocols
- Encryption
- Video/audio calls
- Screen sharing
- Large file transfers
π οΈ Running the Examplesβ
Prerequisitesβ
- Install dependencies:
npm install
- For Node.js examples, you might need wrtc:
npm install wrtc
Running in Node.jsβ
# Run basic example
npm run example
# Run specific examples
npm run example:2peers
npm run example:talk
Running in Browserβ
For browser examples, check out our Playground where you can test the SDK features directly in your browser.
π‘ Tipsβ
- Start Simple: Begin with the basic-usage example to understand core concepts
- Use Debug Mode: Enable
debug: trueto see detailed logs - Check the Playground: Try features interactively in the Playground
- Read the Source: Example source files contain detailed comments
- Join the Community: Share your examples in our Discord
π€ Contributing Examplesβ
Have a cool example to share? We'd love to include it!
- Fork the repository
- Add your example to the
examples/directory - Include clear comments and documentation
- Submit a pull request
Check our Contributing Guide for more details.
π Next Stepsβ
- API Reference - Detailed API documentation
- Guides - In-depth guides for specific use cases
- Playground - Interactive testing environment
- Tutorials - Step-by-step tutorials