NAME

Multi-User Chat using PAGI::WebSocket

SYNOPSIS

pagi-server --app examples/websocket-chat-v2/app.pl --port 5000

DESCRIPTION

This is a port of the examples/10-chat-showcase application that demonstrates how PAGI::WebSocket simplifies WebSocket handling.

The HTTP, SSE, and State modules are identical to the original. Only the WebSocket handler is rewritten to use PAGI::WebSocket.

Compare lib/ChatApp/WebSocket.pm with the original at examples/10-chat-showcase/lib/ChatApp/WebSocket.pm to see the improvements.

Key Differences

  • WebSocket handler uses PAGI::WebSocket->new wrapper

  • Connection accepted with $ws->accept instead of raw protocol

  • Cleanup registered with $ws->on_close callback

  • Message loop uses $ws->each_json for cleaner iteration

  • Sending uses $ws->send_json instead of raw JSON encoding

ENDPOINTS

Same as the original:

GET / - Chat frontend
GET /api/rooms - List rooms
GET /api/room/:name/history - Room message history
GET /api/stats - Server statistics
WS /ws/chat - WebSocket chat endpoint
SSE /events - Server-Sent Events stream

SEE ALSO

PAGI::WebSocket, "10-chat-showcase" in examples