Skip to main content

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​

  1. Install dependencies:
npm install
  1. 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​

  1. Start Simple: Begin with the basic-usage example to understand core concepts
  2. Use Debug Mode: Enable debug: true to see detailed logs
  3. Check the Playground: Try features interactively in the Playground
  4. Read the Source: Example source files contain detailed comments
  5. Join the Community: Share your examples in our Discord

🀝 Contributing Examples​

Have a cool example to share? We'd love to include it!

  1. Fork the repository
  2. Add your example to the examples/ directory
  3. Include clear comments and documentation
  4. Submit a pull request

Check our Contributing Guide for more details.

πŸ“– Next Steps​