Send and receive real-time updates without writing any SSE server-side logic — just POST to sse-hub to publish, and connect to it to subscribe.
Run sse-hub:
./sse-hubConfigure via environment variables:
PORT- Server port (default: 8080)CLIENT_BUFFER- Message buffer size per client (default: 10)CORS_ORIGIN- CORS origin (default: *)
CLIENT_BUFFER explained: Each connected client has a message buffer. If a client is slow (poor connection, browser lag), messages queue in this buffer. When full, the server drops messages or disconnects the client. Larger buffer = more tolerance for slow clients but uses more memory.
Example:
PORT=3000 CLIENT_BUFFER=50 ./sse-hubOpen docs/examples/frontend/demo.html in your browser for an interactive demo showcasing:
- 📊 System Monitoring - Real-time server metrics (CPU, memory, users)
- 🔔 Notifications - Push alerts and system notifications
- 💬 Live Chat - Real-time messaging
- 🔗 Connection Test - Basic SSE functionality testing
-
Start the server:
PORT=8090 go run cmd/server/main.go
-
Open the demo:
open docs/examples/frontend/demo.html
-
Click "Connect to SSE" and test the features!
-
Generate test data (optional):
php docs/examples/backend/php/demo_publisher.php
<!DOCTYPE html>
<html>
<head>
<title>SSE Example</title>
</head>
<body>
<div id="messages"></div>
<script>
const eventSource = new EventSource('http://localhost:8090/events');
eventSource.onmessage = function(event) {
const data = JSON.parse(event.data);
document.getElementById('messages').innerHTML +=
'<div>' + data.message + '</div>';
};
</script>
</body>
</html>How to use in backend (example in php)
<?php declare(strict_types=1);
set_time_limit(0);
function publishSseMessage(int $index): void
{
$data = [
'message' => 'SSE message ' . $index,
'mem' => memory_get_usage(),
'cpu' => sys_getloadavg()[0],
];
$ch = curl_init('http://127.0.0.1:8080/publish');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($data, JSON_THROW_ON_ERROR),
]);
$response = curl_exec($ch);
if ($response === false) {
fwrite(STDERR, 'cURL error: ' . curl_error($ch) . PHP_EOL);
}
curl_close($ch);
}
function run(): void
{
$iterations = 1_000_000;
for ($i = 0; $i < $iterations; $i++) {
sleep(1);
publishSseMessage($i);
}
}
run();