Yaj SSE
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:
- Getting Started - Installation and basic usage
- API Reference - Constructor, methods, and parameters
- Event Handlers - Available events and custom events
- Configuration Options - Options and custom headers
- Examples - Practical code examples
- 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