Skip to main content

Yaj SSE

Build Status Opensource ByJG GitHub source GitHub license GitHub release

A lightweight (<2kb) Yaj Plugin for Server-Sent Events (SSE) EventSource Polyfill. This plugin tries to use the native EventSource object if it is supported by the browser. If there is no native support, the request is made by AJAX requests (polling). You do not need to change the server side nor the client side.

Quick Start

Client Side

var sse = new SSE('http://example.com/sse-server.php', {
onMessage: function(e){
console.log("Message:", e.data);
}
});
sse.start();

Server Side

echo "data: My Message\n";
echo "\n";

Documentation

Comprehensive documentation is available in the /docs folder:

  1. Getting Started - Installation and basic usage
  2. API Reference - Constructor, methods, and parameters
  3. Event Handlers - Available events and custom events
  4. Configuration Options - Options and custom headers
  5. Examples - Practical code examples
  6. Known Limitations & Quirks - Important behavior notes

Installation

Download the repository and include the script:

<script src="yaj-sse.min.js"></script>

Or install via package manager:

yarn add yaj-sse
# or
npm install yaj-sse

Key Features

  • Automatic Fallback: Uses native EventSource when available, falls back to AJAX polling
  • Custom Headers: Support for authentication and custom headers
  • Custom Events: Handle multiple event types from the server
  • Lightweight: Less than 2kb minified
  • Zero Configuration: Works out of the box with sensible defaults

Dependencies

  • Yaj (only required for AJAX fallback)

Development

Minify

npm run minify

Running Examples

Start the web server:

docker run -it --rm -p 8080:80 -v $PWD:/var/www/html byjg/php:7.4-fpm-nginx

Open the browser: http://localhost:8080/examples/sse-client.html


Open source ByJG