|
| 1 | +# PubNub Configuration Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The blockchain application uses PubNub for real-time peer-to-peer communication between nodes. PubNub enables the publish/subscribe pattern that allows blockchain nodes to: |
| 6 | + |
| 7 | +- Broadcast newly mined blocks to all peers |
| 8 | +- Share transactions across the network |
| 9 | +- Synchronize blockchain state in real-time |
| 10 | + |
| 11 | +## Getting Your PubNub Keys |
| 12 | + |
| 13 | +1. **Create a PubNub Account** |
| 14 | + - Visit [https://www.pubnub.com/](https://www.pubnub.com/) |
| 15 | + - Sign up for a free account (no credit card required) |
| 16 | + |
| 17 | +2. **Create a New App** |
| 18 | + - Once logged in, click "Create New App" |
| 19 | + - Give it a name like "python-blockchain" |
| 20 | + |
| 21 | +3. **Get Your Keys** |
| 22 | + - You'll see two important keys: |
| 23 | + - **Publish Key** - Used to send messages to channels |
| 24 | + - **Subscribe Key** - Used to receive messages from channels |
| 25 | + - Keep these keys handy! |
| 26 | + |
| 27 | +### Using secrets.env (Recommended for Production) |
| 28 | + |
| 29 | +1. **Copy the secrets template:** |
| 30 | + ```bash |
| 31 | + cp env.example .env |
| 32 | + ``` |
| 33 | + |
| 34 | +2. **Edit secrets.env and add your keys:** |
| 35 | + ```bash |
| 36 | + PUBNUB_PUBLISH_KEY=pub-c-your-actual-publish-key |
| 37 | + PUBNUB_SUBSCRIBE_KEY=sub-c-your-actual-subscribe-key |
| 38 | + PUBNUB_USER_ID=blockchain-node-1 |
| 39 | + ``` |
| 40 | + |
| 41 | +### Backend Code |
| 42 | + |
| 43 | +The `backend/pubsub.py` file now reads configuration from environment variables. |
| 44 | + |
| 45 | +## Multi-Node Configuration |
| 46 | + |
| 47 | +When running multiple nodes, ensure each has a unique `PUBNUB_USER_ID`: |
| 48 | + |
| 49 | +```bash |
| 50 | +# Node 1 (main) |
| 51 | +export PUBNUB_USER_ID=blockchain-node-1 |
| 52 | +python3 -m backend.app |
| 53 | + |
| 54 | +# Node 2 (peer) - in a different terminal |
| 55 | +export PUBNUB_USER_ID=blockchain-node-2 |
| 56 | +export PEER=True |
| 57 | +export PEER_PORT=5001 |
| 58 | +python3 -m backend.app |
| 59 | + |
| 60 | +# Node 3 (seeded) - in another terminal |
| 61 | +export PUBNUB_USER_ID=blockchain-node-3 |
| 62 | +export PEER=True |
| 63 | +export SEED_DATA=True |
| 64 | +export PEER_PORT=5002 |
| 65 | +python3 -m backend.app |
| 66 | +``` |
| 67 | + |
| 68 | +Each node should have a unique user_id: |
| 69 | +- `blockchain-node-1` - Main node |
| 70 | +- `blockchain-peer-1` - Peer node |
| 71 | +- `blockchain-seed-1` - Seeded node |
| 72 | + |
| 73 | +## PubNub Dashboard Monitoring |
| 74 | + |
| 75 | +You can monitor real-time activity in the PubNub dashboard: |
| 76 | + |
| 77 | +1. Log in to [https://admin.pubnub.com/](https://admin.pubnub.com/) |
| 78 | +2. Select your app |
| 79 | +3. Go to "Debug Console" |
| 80 | +4. Subscribe to your channels to see messages in real-time |
| 81 | + |
| 82 | +This is useful for: |
| 83 | +- Debugging connection issues |
| 84 | +- Monitoring message flow |
| 85 | +- Verifying blockchain synchronization |
| 86 | + |
| 87 | +### Channels Used by This Application |
| 88 | + |
| 89 | +The blockchain application uses the following channels: |
| 90 | + |
| 91 | +- **`BLOCK`** - Broadcasts newly mined blocks to all nodes |
| 92 | +- **`TRANSACTION`** - Shares new transactions across the network |
| 93 | +- **`TEST`** - Used for testing connectivity (development only) |
| 94 | + |
| 95 | +## Quick Start Guide |
| 96 | + |
| 97 | +### First Time Setup |
| 98 | + |
| 99 | +1. **Get your PubNub keys:** |
| 100 | + - Sign up at https://www.pubnub.com/ |
| 101 | + - Create a new app |
| 102 | + - Copy your Publish and Subscribe keys |
| 103 | + |
| 104 | +2. **Configure your environment:** |
| 105 | + ```bash |
| 106 | + # Copy the example file |
| 107 | + cp env.example .env |
| 108 | + |
| 109 | + # Edit .env with your actual keys |
| 110 | + nano .env |
| 111 | + ``` |
| 112 | + |
| 113 | +3. **Run the application:** |
| 114 | + ```bash |
| 115 | + python3 -m backend.app |
| 116 | + ``` |
| 117 | + |
| 118 | +### Running Multiple Nodes |
| 119 | + |
| 120 | +To test blockchain synchronization with multiple nodes: |
| 121 | + |
| 122 | +```bash |
| 123 | +# Terminal 1 - Main node |
| 124 | +export PUBNUB_USER_ID=node-1 |
| 125 | +python3 -m backend.app |
| 126 | + |
| 127 | +# Terminal 2 - Peer node |
| 128 | +export PUBNUB_USER_ID=peer-1 |
| 129 | +export PEER=True |
| 130 | +export PEER_PORT=5001 |
| 131 | +python3 -m backend.app |
| 132 | + |
| 133 | +``` |
| 134 | + |
| 135 | +## Resources |
| 136 | + |
| 137 | +- [PubNub Python SDK Documentation](https://www.pubnub.com/docs/sdks/python) |
| 138 | +- [PubNub Publish/Subscribe Tutorial](https://www.pubnub.com/tutorials/python/) |
| 139 | +- [PubNub Best Practices](https://www.pubnub.com/docs/general/security/best-practices) |
| 140 | +- [PubNub Free Tier Limits](https://www.pubnub.com/pricing/) |
0 commit comments