04 – WebSocket Echo
Handles the WebSocket protocol:
- Waits for
websocket.connect. - Sends
websocket.acceptto complete the handshake. - Echoes incoming frames back via
websocket.send— text frames are echoed with anecho:prefix, binary frames are echoed unchanged. - Skips any
websocket.receiveframe that carries neithertextnorbytes, and stops the loop only whenwebsocket.disconnectarrives.
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
# => echo: Hello
# Type: PAGI WebSocket!
# => echo: 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
Covered by the PAGI specification in the upstream PAGI distribution
(PAGI::Spec POD and protocol documents, https://github.com/jjn1056/pagi):
- WebSocket scope & events