All Projects → hhxsv5 → Php Sse

hhxsv5 / Php Sse

Licence: mit
A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than Websocket, instead of AJAX request.

Projects that are alternatives of or similar to Php Sse

Eventsource
EventSource client for Node.js and Browser (polyfill)
Stars: ✭ 541 (+128.27%)
Mutual labels:  eventsource, sse, server-sent-events
Demo Spring Sse
'Server-Sent Events (SSE) in Spring 5 with Web MVC and Web Flux' article and source code.
Stars: ✭ 102 (-56.96%)
Mutual labels:  eventsource, sse, server-sent-events
http-event-stream
📡 Modern spec-compliant Server Sent Events stream implementation.
Stars: ✭ 16 (-93.25%)
Mutual labels:  events, sse, server-sent-events
sse
HTML5 Server-Sent-Events for Go
Stars: ✭ 84 (-64.56%)
Mutual labels:  events, sse, server-sent-events
sseclient
Pure-Python Server Side Events (SSE) client
Stars: ✭ 85 (-64.14%)
Mutual labels:  sse, server-sent-events
go-sse
Server-Sent Events for Go
Stars: ✭ 106 (-55.27%)
Mutual labels:  sse, server-sent-events
Sapphiredb
SapphireDb Server, a self-hosted, easy to use realtime database for Asp.Net Core and EF Core
Stars: ✭ 326 (+37.55%)
Mutual labels:  sse, server-sent-events
Golang Sse Todo
golang server sent events (sse) example
Stars: ✭ 23 (-90.3%)
Mutual labels:  sse, server-sent-events
sseserver
🏄 High-performance Server-Sent Events endpoint for Go
Stars: ✭ 88 (-62.87%)
Mutual labels:  sse, eventsource
Swell
Swell: API development tool that enables developers to test endpoints served over streaming technologies including Server-Sent Events (SSE), WebSockets, HTTP2, GraphQL, and gRPC.
Stars: ✭ 517 (+118.14%)
Mutual labels:  sse, server-sent-events
Jquery Sse
jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill
Stars: ✭ 37 (-84.39%)
Mutual labels:  eventsource, server-sent-events
go-gin-web-server
Deploy Go Gin on Render
Stars: ✭ 23 (-90.3%)
Mutual labels:  sse, server-sent-events
ruby-eventsource
Server-sent events (SSE) client implementation for Ruby
Stars: ✭ 19 (-91.98%)
Mutual labels:  server-sent-events, eventsource
Eventsource
The Hoa\Eventsource library.
Stars: ✭ 99 (-58.23%)
Mutual labels:  eventsource, server-sent-events
Aiohttp Sse
Server-sent events support for aiohttp
Stars: ✭ 125 (-47.26%)
Mutual labels:  eventsource, server-sent-events
okhttp-eventsource
Server-sent events (SSE) client implementation for Java, based on OkHttp: http://javadoc.io/doc/com.launchdarkly/okhttp-eventsource
Stars: ✭ 70 (-70.46%)
Mutual labels:  server-sent-events, eventsource
Unifrost
Making it easier to push pubsub events directly to the browser.
Stars: ✭ 166 (-29.96%)
Mutual labels:  eventsource, sse
Demo.AspNetCore.ServerSentEvents
Demo project for demonstrating functionality of Lib.AspNetCore.ServerSentEvents
Stars: ✭ 52 (-78.06%)
Mutual labels:  sse, server-sent-events
geo-smart-system
Open Source Realtime Tracking System
Stars: ✭ 36 (-84.81%)
Mutual labels:  sse, server-sent-events
Axway Amplify Streams Js
AMPLIFY Streams Javascript package containing SDK, documentation and sample applications
Stars: ✭ 79 (-66.67%)
Mutual labels:  sse, server-sent-events

PHP SSE: Server-sent Events

A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than Websocket, instead of AJAX request.

Requirements

  • PHP 5.4 or later

Installation via Composer(packagist)

composer require "hhxsv5/php-sse:~2.0" -vvv

Usage

Run demo

  • Run PHP webserver
cd examples
php -S 127.0.0.1:9001 -t .
  • Open url http://127.0.0.1:9001/index.html

Demo

Javascript demo

Client: receiving events from the server.

// withCredentials=true: pass the cross-domain cookies to server-side
const source = new EventSource('http://127.0.0.1:9001/sse.php', {withCredentials: true});
source.addEventListener('news', function (event) {
    console.log(event.data);
    // source.close(); // disconnect stream
}, false);

PHP demo

Server: Sending events by pure php.

use Hhxsv5\SSE\Event;
use Hhxsv5\SSE\SSE;
use Hhxsv5\SSE\StopSSEException;

// PHP-FPM SSE Example: push messages to client

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('Connection: keep-alive');
header('X-Accel-Buffering: no'); // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications

$callback = function () {
    $id = mt_rand(1, 1000);
    $news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
    if (empty($news)) {
        return false; // Return false if no new messages
    }
    $shouldStop = false; // Stop if something happens or to clear connection, browser will retry
    if ($shouldStop) {
        throw new StopSSEException();
    }
    return json_encode(compact('news'));
    // return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
    // return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
};
(new SSE(new Event($callback, 'news')))->start();

Symfony and Laravel demo

Server: Sending events by Laravel or Symfony.

use Hhxsv5\SSE\SSE;
use Hhxsv5\SSE\Event;
use Hhxsv5\SSE\StopSSEException;

// Action method in controller
public function getNewsStream()
{
    $response = new \Symfony\Component\HttpFoundation\StreamedResponse();
    $response->headers->set('Content-Type', 'text/event-stream');
    $response->headers->set('Cache-Control', 'no-cache');
    $response->headers->set('Connection', 'keep-alive');
    $response->headers->set('X-Accel-Buffering', 'no'); // Nginx: unbuffered responses suitable for Comet and HTTP streaming applications
    $response->setCallback(function () {
        $callback = function () {
            $id = mt_rand(1, 1000);
            $news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
            if (empty($news)) {
                return false; // Return false if no new messages
            }
            $shouldStop = false; // Stop if something happens or to clear connection, browser will retry
            if ($shouldStop) {
                throw new StopSSEException();
            }
            return json_encode(compact('news'));
            // return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
            // return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
        };
        (new SSE(new Event($callback, 'news')))->start();
    });
    return $response;
}

Swoole demo

Server: Sending events by Swoole Coroutine Http Server. Install Swoole 4.5.x: pecl install swoole.

use Hhxsv5\SSE\Event;
use Hhxsv5\SSE\SSESwoole;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use Hhxsv5\SSE\StopSSEException;

// Swoole SSE Example: push messages to client

$server = new Server('0.0.0.0', 5200);
$server->set([
    'enable_coroutine'   => true,
    'max_coroutine'      => 10000, // worker_num*10000
    'reactor_num'        => swoole_cpu_num() * 2,
    'worker_num'         => swoole_cpu_num() * 2,
    'max_request'        => 100000,
    'buffer_output_size' => 4 * 1024 * 1024, // 4MB
    'log_level'          => SWOOLE_LOG_WARNING,
    'log_file'           => __DIR__ . '/swoole.log',
]);

$server->on('Request', function (Request $request, Response $response) use ($server) {
    $response->header('Access-Control-Allow-Origin', '*');
    $response->header('Content-Type', 'text/event-stream');
    $response->header('Cache-Control', 'no-cache');
    $response->header('Connection', 'keep-alive');
    $response->header('X-Accel-Buffering', 'no');

    $event = new Event(function () {
        $id = mt_rand(1, 1000);
        $news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service.
        if (empty($news)) {
            return false; // Return false if no new messages
        }
        $shouldStop = false; // Stop if something happens or to clear connection, browser will retry
        if ($shouldStop) {
            throw new StopSSEException();
        }
        return json_encode(compact('news'));
        // return ['event' => 'ping', 'data' => 'ping data']; // Custom event temporarily: send ping event
        // return ['id' => uniqid(), 'data' => json_encode(compact('news'))]; // Custom event Id
    }, 'news');
    (new SSESwoole($event, $request, $response))->start();
});
$server->start();

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].