# Starting Your Peer-to-Peer Messaging App

Canonical: https://docs.projectbabbage.com/docs/quickstarts/starting-peer-to-peer-messaging-app

A step-by-step tutorial for quickly creating a basic messaging application on BSV. This guide shows you how to integrate MessageBox Server (using the public endpoint or your own instance) and MessageBox Client (installed via npm) to build a real-time, peer-to-peer messaging app.

So you’ve got a BSV wallet, you’ve tried a few transactions, and now you’re ready to bring your own idea to life! This guide walks you through bootstrapping a minimal BSV application that leverages open BRC standards for seamless interoperability. Whether you’re building a simple proof-of-concept or aiming for production, these steps will help you get started quickly.
________________________________________
### 1. Prerequisites

- **A BSV-Compatible Wallet:**  
  You’ll use it for key management, signing, and authentication.

- **Node.js & npm (or Yarn):**  
  Essential for developing your app and managing dependencies.

- **MessageBox Server:**  
  Run your own server or use an environment-specific hosted server supplied by your project. The examples use `https://your-messagebox.example` as a placeholder.

- **MessageBox Client:**  
  Install the messaging client library with:
  ```bash
  npm install @bsv/message-box-client
```
- **Basic Knowledge of JavaScript/TypeScript:**
Familiarity with Node.js development is assumed.
________________________________________

### 2. Set Up Your Project
1. **Initialize a New Project:**
```bash
mkdir my-messaging-app && cd my-messaging-app
npm init -y
```

2. **Install Dependencies:**
```bash
npm install @bsv/message-box-client
```
3. **Folder Structure (example):**
my-messaging-app/
├─ package.json
├─ index.js
└─ …
 
________________________________________

3. **Building the Core Messaging Functionality**
Below is a simplified example demonstrating how to initialize the MessageBox Client, send a message, and listen for incoming messages. This snippet abstracts the complexity of connecting via WebSocket with HTTP fallback.

```ts
import { MessageBoxClient } from '@bsv/message-box-client'  // Installed via npm
import { WalletClient } from '@bsv/sdk'

// Example walletClient initialization (adjust as needed)
const walletClient = new WalletClient('auto')

// Instantiate the MessageBox Client by passing in the configured walletClient.
const messageBoxClient = new MessageBoxClient({
  walletClient,
  enableLogging: true  // Enable logging for debugging (off by default)
})

// Initialize the client to fetch your identity key and set up a WebSocket connection.
await messageBoxClient.initializeConnection()

// Send a test message.
await messageBoxClient.sendMessage({
  recipient: 'recipient_public_key',
  messageBox: 'payment_inbox',
  body: { text: 'Hello, peer-to-peer world!' }
})

// Listen for live messages.
await messageBoxClient.listenForLiveMessages({
  messageBox: 'payment_inbox',
  onMessage: (msg) => {
    console.log('Received message:', msg)
  }
})
```

________________________________________


**4. Connecting to MessageBox Server**
Configure your app with the MessageBox Server host you operate or trust.
If you run your own instance locally:
- Follow the setup instructions provided in the [MessageBox Server documentation](https://github.com/bsv-blockchain/message-box-server).
- Update the host property in the MessageBox Client configuration accordingly.
________________________________________
**5. Testing Your App**
1. Run Your Application:
```bash
node index.js
```
2. Verify Functionality:
- Check your console logs for confirmation of a successful WebSocket connection.
- Confirm that outgoing messages are sent.
- Verify that incoming messages are received (either live via WebSocket or via HTTP fallback).
________________________________________
**6. Next Steps & Resources**
- **Enhance Your Messaging Flow:**
Add robust error handling, improve the UI, or integrate message encryption.
- **Expand to Payments:**
Check out the “Sending Micropayments Through Your Peer-to-Peer App” guide to integrate PeerPay for real-time Bitcoin payments.
- **Further Reading:**
  - [MessageBox Server Documentation](https://github.com/bsv-blockchain/message-box-server)
  - `@bsv/message-box-client` [npm Package](https://github.com/bsv-blockchain/ts-stack/tree/main/packages/messaging/message-box-client)
________________________________________
### Conclusion
You now have a minimal peer-to-peer messaging app on BSV using MessageBox Server and MessageBox Client. This guide provided an abstract yet practical overview of setting up your project, integrating the messaging libraries, and testing real-time message flows. Use this foundation to expand your app’s capabilities and explore advanced features like message encryption or micropayments.

Happy building and enjoy creating your peer-to-peer messaging experience on BSV!
