Skip to content

Commit 67399fe

Browse files
committed
Update pubnub config
1 parent 086ce30 commit 67399fe

7 files changed

Lines changed: 174 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
blockchain-env/lib/python3.7
55
*.DS_Store*
66

7+
backend/.env
8+

PUBNUB_CONFIG.md

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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/)

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ python3 -m backend.app
7979
**Run a peer instance**
8080

8181
Make sure to activate the virtual environment.
82+
Choose a unique PUBNUB_USER_ID per peer.
8283

8384
```
84-
export PEER=True && python3 -m backend.app
85+
export PEER=True && export PUBNUB_USER_ID=blockchain-peer-1 && python3 -m backend.app
8586
```
8687

8788
**Run the frontend**
@@ -97,4 +98,13 @@ Make sure to activate the virtual environment.
9798

9899
```
99100
export SEED_DATA=True && python3 -m backend.app
100-
```
101+
```
102+
103+
** PubNub Configuration**
104+
105+
This application uses PubNub for real-time peer-to-peer communication between blockchain nodes. **You must configure PubNub to run the application.**
106+
107+
**See [PUBNUB_CONFIG.md](PUBNUB_CONFIG.md) for detailed setup instructions.**
108+
109+
1. Get free PubNub keys at [https://www.pubnub.com/](https://www.pubnub.com/)
110+
2. copy `backend/env.example` to `backend/.env` and configure it with your keys.

backend/app/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import os
22
import requests
33
import random
4+
from pathlib import Path
5+
from dotenv import load_dotenv
6+
7+
# Load environment variables FIRST, before any other backend imports
8+
env_path = Path(__file__).parent.parent / '.env'
9+
load_dotenv(dotenv_path=env_path)
410

511
from flask import Flask, jsonify, request
612
from flask_cors import CORS

backend/env.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Environment Configuration (Development)
2+
# Copy this file to .env and update with your actual values
3+
4+
# PubNub Configuration
5+
# Get your keys from: https://www.pubnub.com/
6+
PUBNUB_PUBLISH_KEY=pub-c-your-actual-publish-key
7+
PUBNUB_SUBSCRIBE_KEY=sub-c-your-actual-subscribe-key
8+
PUBNUB_USER_ID=blockchain-node-1
9+

backend/pubsub.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import time
23

34
from pubnub.pubnub import PubNub
@@ -8,10 +9,9 @@
89
from backend.wallet.transaction import Transaction
910

1011
pnconfig = PNConfiguration()
11-
# NOTE: these keys have been retired.
12-
pnconfig.subscribe_key = 'sub-c-448d1fb4-5f47-4e56-a496-e4588e0808a7'
13-
pnconfig.publish_key = 'pub-c-691add7b-2cc0-42c8-96f9-7edf786b5b4b'
14-
pnconfig.user_id = 'test-1'
12+
pnconfig.publish_key = os.environ.get('PUBNUB_PUBLISH_KEY')
13+
pnconfig.subscribe_key = os.environ.get('PUBNUB_SUBSCRIBE_KEY')
14+
pnconfig.user_id = os.environ.get('PUBNUB_USER_ID', 'blockchain-node-default')
1515

1616
CHANNELS = {
1717
'TEST': 'TEST',

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Flask-Cors==3.0.8
44
pubnub==10.4.1
55
requests==2.32.5
66
cryptography==46.0.3
7+
python-dotenv==1.0.0

0 commit comments

Comments
 (0)