Skip to main content
Lyger includes a built-in WebSocket server for real-time, bidirectional communication between server and clients. Perfect for chat applications, live notifications, collaborative editing, and real-time dashboards.

WebSocket Server

Create a WebSocket server by extending the WebSocket class:
The WebSocket server runs in a separate process. Use php websocket.php to start it independently from your main application.

Basic Operations

Broadcasting

Send messages to all connected clients:

Channels

Group clients into channels:

Leave Channel

Client Connection

JavaScript Client

PHP Client

Use WebSocketClient to connect from PHP:

Advanced Features

Authentication

Authenticate clients before allowing connections:

Rate Limiting

Prevent spam with rate limiting:

Ping/Pong Heartbeat

Keep connections alive with heartbeats:

Real-World Examples

Chat Application

Live Dashboard

Collaborative Editor

Running WebSocket Server

Standalone Script

Run with:
The WebSocket server blocks the process. Run it in a separate terminal or use a process manager like Supervisor to keep it running in production.

With Supervisor

Performance Tips

Batch multiple updates:
Limit connections per IP:

Next Steps

Events

Event broadcasting system

Cache

High-performance caching