Server Guide

Building Server-Side MessageBox Applications with BSV SDK

This guide covers server-side patterns for implementing MessageBox functionality using Node.js/Express, including wallet session management, certification endpoints, and database storage.

📦 Complete Code Repository: https://github.com/bsv-blockchain-demos/messagebox-platformarrow-up-right

All code examples in this guide are taken from the working implementation in the server/ directory.


Table of Contents


Setup

Installation

Project Structure

Environment Configuration

Create .env:

TypeScript Configuration

tsconfig.json:


Session Management

Wallet Session Manager

Manages wallet sessions with 30-minute timeout and automatic cleanup:

📁 See full implementation: server/src/wallet/WalletSessionManager.tsarrow-up-right

Key Features:

  • 30-minute session timeout

  • Auto-cleanup every 5 minutes

  • Session refresh on each access

  • Singleton pattern for global state


Database Storage

MongoDB Certification Storage

Store and query certified user identities:

📁 See full implementation: server/src/storage/CertificationStorage.tsarrow-up-right

Database Schema:


Certification Endpoint

Store Certification Route

📁 See full implementation: server/src/routes/certify.tsarrow-up-right


Wallet Endpoints

Wallet Connection Routes

📁 See full implementation: server/src/routes/wallet.tsarrow-up-right


Payment Endpoints

Payment Initiation Route (Optional)

📁 See full implementation: server/src/routes/payment.tsarrow-up-right

Note: This endpoint only validates the recipient. Actual payment transactions are created and broadcast on the frontend using WalletClient and PeerPayClient for security.


Complete Server

Main Server Setup

📁 See full implementation: server/src/index.tsarrow-up-right


Testing

Start MongoDB

Start Server

Test Endpoints


Deployment Considerations

Environment Variables

Production .env:

Security Best Practices

  1. HTTPS Only: Use SSL/TLS certificates

  2. Rate Limiting: Prevent abuse with rate limits

  3. Input Validation: Validate all user inputs

  4. CORS: Restrict origins in production

  5. Session Secrets: Use strong random session IDs

  6. Database Credentials: Secure MongoDB with authentication

Scaling Considerations

  1. Session Storage: Use Redis for distributed sessions

  2. Database Replication: MongoDB replica sets for redundancy

  3. Load Balancing: Distribute requests across multiple servers

  4. Caching: Cache certified users list with TTL

  5. Monitoring: Add logging and metrics


Summary

This server guide covered:

  • Session Management - Managing wallet sessions with expiration

  • Database Storage - MongoDB for certified user storage

  • API Endpoints - RESTful routes for wallet, certification, and payments

  • Security Patterns - Session validation and authentication

  • Deployment - Production considerations and best practices

These patterns provide a production-ready backend for MessageBox applications.


Next Steps

Last updated