# Getting Started with Commy - Practical Tutorial
This guide teaches you how to **actually use Commy** with working examples.
---
## What We'll Build
A simple real-time **weather monitoring system**:
- βοΈ **Weather Sensor** - Reads temperature and updates Commy
- π **Dashboard** - Displays current weather
- β οΈ **Alert System** - Warns if temperature is too high
- π± **Mobile Client** - Views weather from phone
All communicating through Commy!
---
## Step 1: Start the Commy Server
### 1.1: Generate Certificates (One-time setup)
```powershell
# Create cert and key files
# (We did this in testing)
# Files: dev-cert-temp.pem, dev-key-temp.pem
```
### 1.2: Start the Server
```powershell
cd C:\Users\cires\OneDrive\Documents\projects\commy
# Set environment variables
$env:COMMY_TLS_CERT_PATH = "dev-cert-temp.pem"
$env:COMMY_TLS_KEY_PATH = "dev-key-temp.pem"
$env:COMMY_LISTEN_ADDR = "127.0.0.1"
$env:COMMY_LISTEN_PORT = "8443"
$env:COMMY_SERVER_ID = "weather-server"
$env:COMMY_CLUSTER_ENABLED = "false"
# Start server
.\target\debug\commy.exe
```
**Expected output:**
```
[Commy] ββββββββββββββββββββββββββββββββββββββββββββ
[Commy] Initializing Commy Server
[Commy] ββββββββββββββββββββββββββββββββββββββββββββ
[Commy] Server ID: weather-server
[Commy] Client listen address: 127.0.0.1:8443
[Commy] TLS enabled: true
[Commy] Clustering enabled: false
[Commy] β TLS initialized successfully (WSS mode)
[Commy] β Server initialized, starting WSS listener...
[Commy] Remote clients connect to: wss://127.0.0.1:8443
```
Server is now running! β
---
## Step 2: Program 1 - Weather Sensor
This program reads temperature and sends it to Commy.
### Create `weather_sensor.js`
```javascript
// weather_sensor.js - Reads temperature and updates Commy
const WebSocket = require('ws');
// Configuration
const COMMY_URL = 'wss://127.0.0.1:8443';
const TENANT = 'weather';
const SERVICE = 'current_conditions';
const API_KEY = 'sensor-key-123';
// Connect to Commy
const ws = new WebSocket(COMMY_URL, {
rejectUnauthorized: false // OK for dev, use real certs in prod
});
// When connected
ws.on('open', async () => {
console.log('β Connected to Commy');
// Step 1: Authenticate
const authRequest = {
type: 'Authenticate',
payload: {
tenant_name: TENANT,
method: 'api_key',
credentials: API_KEY
}
};
ws.send(JSON.stringify(authRequest));
console.log('β Sent authentication request');
});
// Handle responses
ws.on('message', (data) => {
const msg = JSON.parse(data);
if (msg.type === 'AuthResponse') {
if (msg.success) {
console.log('β Authenticated successfully');
startSendingTemperatures();
} else {
console.log('β Authentication failed');
}
}
});
// Simulate reading temperatures
function startSendingTemperatures() {
let temp = 72; // Start at 72Β°F
setInterval(() => {
// Simulate temperature change
temp += (Math.random() - 0.5) * 2; // Β±1Β°F
temp = Math.round(temp * 10) / 10; // Round to 1 decimal
// Send temperature to Commy
const setVarRequest = {
type: 'SetVariables',
payload: {
tenant_name: TENANT,
service_name: SERVICE,
variables: {
temperature: Math.round(temp).toString(),
humidity: Math.round(Math.random() * 100).toString(),
timestamp: new Date().toISOString()
}
}
};
ws.send(JSON.stringify(setVarRequest));
console.log(`β Updated: Temp=${temp}Β°F`);
}, 3000); // Update every 3 seconds
}
// Error handling
ws.on('error', (error) => {
console.error('β Error:', error.message);
});
ws.on('close', () => {
console.log('β Disconnected from Commy');
});
```
### Run the Sensor
```bash
# Install dependency
npm install ws
# Run sensor
node weather_sensor.js
```
**Expected output:**
```
β Connected to Commy
β Sent authentication request
β Authenticated successfully
β Updated: Temp=72Β°F
β Updated: Temp=72.8Β°F
β Updated: Temp=73.1Β°F
...
```
β
Your sensor is now updating Commy every 3 seconds!
---
## Step 3: Program 2 - Dashboard Display
This program reads temperature and displays it.
### Create `weather_dashboard.js`
```javascript
// weather_dashboard.js - Shows live weather
const WebSocket = require('ws');
const COMMY_URL = 'wss://127.0.0.1:8443';
const TENANT = 'weather';
const SERVICE = 'current_conditions';
const API_KEY = 'dashboard-key-456';
const ws = new WebSocket(COMMY_URL, {
rejectUnauthorized: false
});
let authenticated = false;
ws.on('open', () => {
console.log('β Connected to Commy');
// Authenticate
const authRequest = {
type: 'Authenticate',
payload: {
tenant_name: TENANT,
method: 'api_key',
credentials: API_KEY
}
};
ws.send(JSON.stringify(authRequest));
});
ws.on('message', (data) => {
const msg = JSON.parse(data);
if (msg.type === 'AuthResponse' && msg.success) {
console.log('β Authenticated');
authenticated = true;
startReadingWeather();
}
// Handle variable change notifications
if (msg.type === 'VariableChanged') {
displayWeather(msg.payload.variables);
}
});
function startReadingWeather() {
console.log('\nπ WEATHER DASHBOARD\n');
// Request initial data
const getVarRequest = {
type: 'GetVariables',
payload: {
tenant_name: TENANT,
service_name: SERVICE
}
};
ws.send(JSON.stringify(getVarRequest));
// Update display every second
setInterval(() => {
ws.send(JSON.stringify(getVarRequest));
}, 1000);
}
function displayWeather(variables) {
console.clear();
console.log('βββββββββββββββββββββββββββββββββ');
console.log('β WEATHER DASHBOARD β');
console.log('βββββββββββββββββββββββββββββββββ\n');
console.log(`π‘οΈ Temperature: ${variables.temperature || '--'}Β°F`);
console.log(`π§ Humidity: ${variables.humidity || '--'}%`);
console.log(`β° Updated: ${variables.timestamp || '--'}\n`);
// Show status
const temp = parseFloat(variables.temperature);
if (temp > 80) {
console.log('β οΈ HOT - Temperature above 80Β°F');
} else if (temp < 60) {
console.log('βοΈ COLD - Temperature below 60Β°F');
} else {
console.log('β
COMFORTABLE - Temperature normal');
}
}
ws.on('error', (error) => {
console.error('β Error:', error.message);
});
```
### Run the Dashboard
In a **second terminal**:
```bash
node weather_dashboard.js
```
**Expected output:**
```
β Connected to Commy
β Authenticated
βββββββββββββββββββββββββββββββββ
β WEATHER DASHBOARD β
βββββββββββββββββββββββββββββββββ
π‘οΈ Temperature: 72.8Β°F
π§ Humidity: 45%
β° Updated: 2026-02-15T07:45:23.123Z
β
COMFORTABLE - Temperature normal
```
β
Dashboard is now displaying live temperature!
---
## Step 4: Program 3 - Alert System
Sends alerts when temperature is too high.
### Create `weather_alerts.js`
```javascript
// weather_alerts.js - Sends alerts for extreme temperatures
const WebSocket = require('ws');
const COMMY_URL = 'wss://127.0.0.1:8443';
const TENANT = 'weather';
const SERVICE = 'current_conditions';
const API_KEY = 'alert-key-789';
const TEMP_THRESHOLD_HIGH = 85; // Alert if above
const TEMP_THRESHOLD_LOW = 55; // Alert if below
const ws = new WebSocket(COMMY_URL, {
rejectUnauthorized: false
});
let lastAlertTime = 0;
const ALERT_COOLDOWN = 5000; // 5 second cooldown between alerts
ws.on('open', () => {
console.log('β Alert system connected');
ws.send(JSON.stringify({
type: 'Authenticate',
payload: {
tenant_name: TENANT,
method: 'api_key',
credentials: API_KEY
}
}));
});
ws.on('message', (data) => {
const msg = JSON.parse(data);
if (msg.type === 'AuthResponse' && msg.success) {
console.log('β Alert system authenticated\n');
subscribeToWeather();
}
// Check for temperature changes
if (msg.type === 'VariableChanged' && msg.payload.variables) {
checkTemperature(msg.payload.variables);
}
});
function subscribeToWeather() {
ws.send(JSON.stringify({
type: 'Subscribe',
payload: {
tenant_name: TENANT,
service_name: SERVICE,
variables: ['temperature']
}
}));
}
function checkTemperature(variables) {
const temp = parseFloat(variables.temperature);
const now = Date.now();
// Avoid spamming alerts
if (now - lastAlertTime < ALERT_COOLDOWN) {
return;
}
if (temp > TEMP_THRESHOLD_HIGH) {
sendAlert('π₯', `HOT ALERT: ${temp}Β°F (threshold: ${TEMP_THRESHOLD_HIGH}Β°F)`);
lastAlertTime = now;
} else if (temp < TEMP_THRESHOLD_LOW) {
sendAlert('βοΈ ', `COLD ALERT: ${temp}Β°F (threshold: ${TEMP_THRESHOLD_LOW}Β°F)`);
lastAlertTime = now;
}
}
function sendAlert(emoji, message) {
console.log(`${emoji} ALERT: ${message}`);
// In production, you could:
// - Send email
// - Send SMS
// - Post to Slack
// - Trigger automation
}
ws.on('error', (error) => {
console.error('β Error:', error.message);
});
```
### Run the Alert System
In a **third terminal**:
```bash
node weather_alerts.js
```
**Expected output:**
```
β Alert system connected
β Alert system authenticated
π₯ ALERT: Hot temperature: 85.3Β°F (threshold: 85Β°F)
βοΈ ALERT: Cold temperature: 54.8Β°F (threshold: 55Β°F)
```
---
## How It All Works Together
```
ββββββββββββββββββββββββββββββββββββββββββββββββββββ
β COMMY SERVER β
β (wss://127.0.0.1:8443) β
ββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββ
β β
ββββββββββββββ΄βββββββ βββββββββ΄βββββββ
β β β β
βΌ βΌ βΌ βΌ
βββββββββββββββ ββββββββββββββββββ βββββββββββ
β SENSOR β β DASHBOARD β β ALERTS β
β (Writes) β β (Reads every β β(Watches)β
β β β second) β β β
βTemperature: βββββββββββββββββββββββββββββββ
β 72Β°F β SERVICE: β
β 73Β°F β current_conditions β
β 74Β°F ββββββββββββββββββββββββββββββΊβ
β ... β β
βββββββββββββββ ββββββββββββββββββββββββββ βββββββββββ
Timeline:
T+0s: Sensor sends 72Β°F
T+1s: Dashboard reads 72Β°F -> displays
T+2s: Sensor sends 73Β°F
T+3s: Dashboard reads 73Β°F -> displays
Alerts checks -> 73Β°F is OK, no alert
T+5s: Sensor sends 85Β°F
Dashboard reads 85Β°F -> displays
Alerts checks -> 85Β°F TOO HIGH! -> ALERT!
```
---
## Step 5: Testing Your System
### Test 1: Basic Functionality
1. β
Start sensor - Should show "Updated: Temp=..."
2. β
Start dashboard - Should show live temperature
3. β
Start alerts - Should show system ready
### Test 2: Data Flow
1. Modify sensor code to send `temp = 90`
2. Watch dashboard update to show 90Β°F
3. Watch alert system trigger HIGH TEMP alert β
### Test 3: Multiple Clients
Open **4 dashboards** - all should show same temperature:
```bash
# Terminal 1
node weather_dashboard.js
# Terminal 2
node weather_dashboard.js
# Terminal 3
node weather_dashboard.js
# Terminal 4
node weather_dashboard.js
```
All 4 dashboards update together! β
---
## Real-World Applications
### Application 1: Stock Trading
Replace "temperature" with "stock price":
```
Sensor β Price Feed (external data)
Dashboard β Trader terminals
Alerts β Risk management (stop losses)
```
### Application 2: IoT Smart Home
```
Sensors: Temperature, humidity, light
Dashboard: Central control panel
Alerts: Notifications on phone
```
### Application 3: Factory Automation
```
Sensors: Machine status, production metrics
Dashboard: Factory floor monitor
Alerts: Maintenance warnings
```
### Application 4: Cloud Infrastructure
```
Sensors: CPU, memory, disk usage
Dashboard: Operations dashboard
Alerts: PagerDuty/Slack notifications
```
---
## Troubleshooting
### Problem: "Connection refused"
**Cause:** Server not running
**Solution:** Start server first in first terminal
```powershell
$env:COMMY_TLS_CERT_PATH = "dev-cert-temp.pem"
$env:COMMY_TLS_KEY_PATH = "dev-key-temp.pem"
.\target\debug\commy.exe
```
### Problem: "Authentication failed"
**Cause:** Wrong tenant name or API key
**Solution:** Check TENANT and API_KEY match config
```javascript
const TENANT = 'weather'; // Must match everywhere
const API_KEY = 'sensor-key-123'; // Use API key method
```
### Problem: "Certificate error"
**Cause:** Invalid dev certificate
**Solution:** Already handled with `rejectUnauthorized: false`
### Problem: Dashboard not updating
**Cause:** Sensor not sending data
**Solution:** Check sensor is still running and authenticated
```bash
# Monitor sensor output
# Should see "β Updated: Temp=XXΒ°F" every 3 seconds
```
---
## Next Steps
### 1. Add More Features
- [ ] Store historical data
- [ ] Add graph visualization
- [ ] Send email alerts
- [ ] Mobile app support
### 2. Move to Production
- [ ] Get real TLS certificates
- [ ] Set up PostgreSQL backend
- [ ] Deploy to cloud server
- [ ] Add monitoring
### 3. Scale Up
- [ ] Monitor 1000+ sensors
- [ ] Add data aggregation
- [ ] Implement caching
- [ ] Set up clustering
### 4. Build SDKs
- [ ] Python SDK
- [ ] C# SDK
- [ ] Go SDK
---
## Challenge: Build Your Own!
Try building one of these:
**Easy:**
- IoT temperature sensor
- System stats monitor
- Price tracker
**Medium:**
- Multi-location monitoring
- Alert rule engine
- Data logger
**Hard:**
- Real-time collaboration (like Google Docs)
- Distributed cache
- Event streaming system
---
## Conclusion
You've learned:
- β
How to start Commy server
- β
How to write sensor program
- β
How to read shared data
- β
How to set up alerts
- β
How multiple programs communicate
**Commy lets you:**
- π Build real-time systems
- π Coordinate multiple services
- π Share live data efficiently
- β‘ Respond instantly to changes
---
**You're ready to use Commy!** π
Need help? Check:
- BEGINNERS_GUIDE.md - Commy concepts
- ARCHITECTURE.md - Technical details
- RUST_BASICS.md - Understanding code