PHP code example of highperapp / realtime
1. Go to this page and download the library: Download highperapp/realtime library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
highperapp / realtime example snippets
// public/index.php
undation\Application;
use HighPerApp\HighPer\Realtime\RealtimeServiceProvider;
$container = new Container();
$router = new Router($container);
$app = new Application($container, $router);
// Real-time features are auto-registered when environment variables are set
// through the HighPer framework's service provider auto-discovery
$app->start();
use HighPerApp\HighPer\Realtime\RealtimeServiceProvider;
$container = new Container();
// Configure real-time settings
$container->bind('config', function() {
return [
'realtime' => [
'enabled' => true,
'protocols' => [
'websocket' => ['enabled' => true],
'sse' => ['enabled' => true]
]
]
];
});
// Register real-time provider
$realtimeProvider = new RealtimeServiceProvider($container);
$realtimeProvider->register();
$realtimeProvider->boot();
// Handle WebSocket connections
$app->websocket('/ws', function($connection, $message) {
// Echo message back to sender
$connection->send(['echo' => $message->getPayload()]);
// Broadcast to all connections
$connection->broadcast(['announcement' => 'New message received']);
// Join/leave channels
$connection->joinChannel('chat-room');
$connection->broadcastToChannel('chat-room', ['user_joined' => $connection->getId()]);
});
// SSE endpoint for live updates
$app->sse('/events', function($stream) {
// Send initial data
$stream->send(['type' => 'welcome', 'data' => 'Connected to live stream']);
// Send periodic updates
$timer = \Amp\repeat(1000, function() use ($stream) {
$stream->send([
'type' => 'update',
'data' => ['timestamp' => time(), 'status' => 'active']
]);
});
});
// Broadcast to all connected clients
$broadcast = $app->broadcast();
// Broadcast to specific channel
$broadcast->toChannel('notifications')->send([
'type' => 'alert',
'message' => 'System maintenance in 5 minutes'
]);
// Broadcast to specific users
$broadcast->toUsers(['user1', 'user2'])->send([
'type' => 'private_message',
'from' => 'admin',
'message' => 'Hello there!'
]);
$container->bind('config', function() {
return [
'realtime' => [
'enabled' => true,
'auto_start' => true,
'server' => [
'host' => '0.0.0.0',
'port' => 8080,
'max_connections' => 10000
],
'protocols' => [
'websocket' => [
'enabled' => true,
'path' => '/ws',
'max_frame_size' => 2097152,
'compression' => true
],
'sse' => [
'enabled' => true,
'path' => '/sse',
'retry_interval' => 3000
]
],
'broadcasting' => [
'default' => 'redis',
'drivers' => [
'redis' => [
'host' => 'localhost',
'port' => 6379
]
]
]
]
];
});
// Before: Just your business logic
$app->websocket('/chat', function($connection, $message) {
$connection->send(['echo' => $message->getPayload()]);
$connection->broadcast(['announcement' => 'New message']);
});
// After telemetry enabled: Same code, but now you get:
// ✅ websocket.connections.total metrics
// ✅ websocket.messages.{total,size} tracking
// ✅ Distributed tracing spans
// ✅ Security monitoring for threats
// ✅ Performance optimization insights
// ✅ Error tracking and alerting
// Check if monitoring library is available
if (class_exists('\\HighPerApp\\HighPer\\Monitoring\\Facades\\Monitor')) {
use HighPerApp\HighPer\Monitoring\Facades\Monitor;
// Custom business metrics
Monitor::increment('orders.completed');
Monitor::gauge('inventory.level', $currentStock);
Monitor::timing('payment.processing', $duration);
// Security events
Monitor::security('failed_login', [
'user_id' => $userId,
'ip' => $clientIp,
'severity' => 'medium'
]);
}
// Check if tracing library is available
if (class_exists('\\HighPerApp\\HighPer\\Tracing\\Facades\\Trace')) {
use HighPerApp\HighPer\Tracing\Facades\Trace;
// Custom tracing spans
Trace::span('user.registration', function() use ($userData) {
return $this->registerUser($userData);
});
}
// Configure JWT authentication
$app->configureRealtime([
'security' => [
'authentication' => [
'enabled' => true,
'driver' => 'jwt',
'jwt' => [
'secret' => 'your-secret-key',
'verify_expiration' => true
]
]
]
]);
// Authenticate connections
$app->websocket('/ws', function($connection) {
$token = $connection->getAuthToken();
$user = JWT::decode($token, 'your-secret-key');
$connection->setUser($user);
$connection->send(['authenticated' => true, 'user' => $user->id]);
});
dockerfile
FROM php:8.3-cli
# Install real-time dependencies
RUN apt-get update && apt-get install -y libssl-dev
RUN pecl install swoole redis
COPY . /app
WORKDIR /app
# Install composer dependencies
RUN composer install --no-dev --optimize-autoloader
# Expose real-time port
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/realtime/health || exit 1
CMD ["php", "public/index.php"]