windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
Documentation
# wschat - Production WebSocket Chat Server

**A scalable real-time chat server built in Windjammer**

Designed to handle 10,000+ concurrent connections with <10ms p50 latency.

---

## ๐Ÿš€ Features

- โœ… **WebSocket** - Real-time bidirectional communication
- โœ… **Rooms** - Create and join chat rooms
- โœ… **Presence** - User online/offline/typing indicators
- โœ… **Message History** - Last 100 messages per room
- โœ… **Authentication** - JWT token support
- โœ… **Rate Limiting** - Prevent abuse (token bucket)
- โœ… **Metrics** - Prometheus metrics + JSON stats
- โœ… **Scalable** - Designed for 10k+ connections

---

## ๐Ÿ“ฆ Installation

```bash
# Build from source
cd examples/wschat
wj build --release

# Run server
./target/release/wschat

# Or use wj run
wj run
```

---

## ๐ŸŽฏ Usage

### Start Server

```bash
# Default (port 8080)
./wschat

# Custom configuration
HOST=0.0.0.0 PORT=3000 MAX_CONNECTIONS=5000 ./wschat
```

### Client Example (JavaScript)

```javascript
// Connect
const ws = new WebSocket('ws://localhost:8080/ws');

// Authenticate (optional)
ws.onopen = () => {
  ws.send(JSON.stringify({
    type: 'auth',
    token: 'YOUR_JWT_TOKEN'
  }));
};

// Join a room
ws.send(JSON.stringify({
  type: 'join',
  room: 'general'
}));

// Send message
ws.send(JSON.stringify({
  type: 'message',
  room: 'general',
  text: 'Hello, world!'
}));

// Receive messages
ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  console.log('Received:', msg);
};
```

---

## ๐Ÿ“‹ Message Protocol

### Client โ†’ Server

```json
// Join room
{"type": "join", "room": "general"}

// Send message
{"type": "message", "room": "general", "text": "Hello!"}

// Leave room
{"type": "leave", "room": "general"}

// Direct message
{"type": "dm", "to": "user123", "text": "Hi!"}

// Typing indicator
{"type": "typing", "room": "general", "status": true}

// Request user list
{"type": "list_users", "room": "general"}

// Request history
{"type": "history", "room": "general", "limit": 50}

// Ping
{"type": "ping"}
```

### Server โ†’ Client

```json
// Welcome
{"type": "welcome", "user_id": "abc", "username": "john"}

// Joined room
{"type": "joined", "room": "general", "users": [...]}

// New message
{"type": "message", "room": "general", "from": "alice", "text": "Hi!", "timestamp": 1234567890}

// User presence
{"type": "presence", "room": "general", "user": "bob", "status": "online"}

// Typing indicator
{"type": "typing", "room": "general", "user": "alice", "status": true}

// Error
{"type": "error", "message": "Room not found"}

// Pong
{"type": "pong"}
```

---

## ๐Ÿ”ง Configuration

Environment variables:

- `HOST` - Server host (default: `0.0.0.0`)
- `PORT` - WebSocket server port (default: `8080`)
- `MAX_CONNECTIONS` - Max concurrent connections (default: `10000`)
- `RATE_LIMIT` - Messages per second per user (default: `10`)
- `HEARTBEAT_INTERVAL` - Heartbeat interval in seconds (default: `30`)
- `ENABLE_PERSISTENCE` - Enable message persistence (default: `false`)
- `DATABASE_URL` - Database URL for persistence (optional)

---

## ๐Ÿ“Š Monitoring

### Prometheus Metrics

Available at `http://localhost:8081/metrics`:

- `wschat_connections_total` - Active connections
- `wschat_rooms_total` - Active rooms
- `wschat_room_members_total` - Total room memberships
- `wschat_uptime_seconds` - Server uptime

### JSON Metrics

Available at `http://localhost:8081/metrics/json`:

```json
{
  "connections": 1234,
  "rooms": 56,
  "total_memberships": 2345,
  "timestamp": 1234567890
}
```

### Health Check

Available at `http://localhost:8080/health` and `http://localhost:8081/health`

---

## ๐Ÿงช Testing

```bash
# Run unit tests
wj test

# Run load test (10k connections)
wj test --test load_test

# Run benchmarks
wj bench
```

---

## โšก Performance

**Targets**:
- **10,000+ concurrent connections**
- **<10ms p50 message latency**
- **100,000 messages/second throughput**
- **<1KB memory per connection**

**Status**: ๐Ÿšง Benchmarks in progress

---

## ๐Ÿ—๏ธ Architecture

```
wschat/
โ”œโ”€โ”€ main.wj         # Server entry point
โ”œโ”€โ”€ server.wj       # Connection handling
โ”œโ”€โ”€ room.wj         # Room management
โ”œโ”€โ”€ message.wj      # Message types
โ”œโ”€โ”€ presence.wj     # User presence
โ”œโ”€โ”€ auth.wj         # JWT authentication
โ”œโ”€โ”€ rate_limit.wj   # Rate limiting (token bucket)
โ””โ”€โ”€ metrics.wj      # Prometheus metrics
```

### Key Design Decisions

1. **Arc<Mutex<>>** for shared state (connections, rooms)
2. **Token bucket** rate limiting per user
3. **In-memory** message history (100 messages per room)
4. **Heartbeat** task per connection (30s interval)
5. **Graceful shutdown** closes all connections cleanly

---

## ๐ŸŽ“ What This Validates

This production WebSocket server validates Windjammer's:

1. **WebSocket Support** - `std.websocket` for real-time
2. **Async/Await at Scale** - 10k+ concurrent connections
3. **Concurrent Data Structures** - `Arc<Mutex<HashMap<>>>`
4. **Channel-Based Messaging** - Broadcast to rooms
5. **Memory Efficiency** - <1KB per connection
6. **Graceful Shutdown** - Clean connection closure

---

## ๐Ÿšง Roadmap

### Phase 1: Core (Week 2) - COMPLETE โœ…
- [x] WebSocket server setup
- [x] Connection handling
- [x] Room management
- [x] Message routing
- [x] User presence
- [x] Authentication
- [x] Rate limiting
- [x] Metrics
- [x] Message persistence (SQLite)

### Phase 2: Features (Week 2-3) - COMPLETE โœ…
- [x] Message persistence with search โœ…
- [x] Direct messages (1-to-1 chat) โœ…
- [x] Connection recovery โœ…
- [x] Heartbeat implementation โœ…

### Phase 3: Performance (Week 3)
- [ ] Load testing (10k connections)
- [ ] Performance profiling
- [ ] Memory optimization
- [ ] Latency benchmarks

### Phase 4: Polish (Week 3)
- [ ] Client libraries (JS, Python, Rust)
- [ ] Complete documentation
- [ ] Docker deployment
- [ ] Kubernetes manifests

---

## ๐Ÿ“„ License

MIT

---

*Built with Windjammer v0.23.0*  
*Part of the v0.23.0 Production Hardening initiative*