04 – WebSocket Echo
Handles the WebSocket protocol:
- Waits for
websocket.connect. - Sends
websocket.acceptto complete the handshake. - Echoes incoming frames back via
websocket.send. - Stops when
websocket.disconnectarrives or whenwebsocket.receivecontains neithertextnorbytes.
Quick Start
1. Start the server:
pagi-server --app examples/04-websocket-echo/app.pl --port 5000
2. Demo with websocat:
# Install websocat if needed
brew install websocat # macOS
# or: cargo install websocat # with Rust
# Connect and send messages
websocat ws://localhost:5000/
# Type: Hello
# => Hello
# Type: PAGI WebSocket!
# => PAGI WebSocket!
3. Or use JavaScript in browser console:
const ws = new WebSocket('ws://localhost:5000/');
ws.onmessage = (e) => console.log('Received:', e.data);
ws.onopen = () => ws.send('Hello from browser!');
Spec References
- WebSocket scope & events –
docs/specs/www.mkdn