PHP Classes
elePHPant
Icontem

PHP Server Sent Events Library: Send data of server events to the browsers

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-11-26 (1 year ago) RSS 2.0 feedStarStarStar 40%Total: 278 All time: 7,317 This week: 543Up
Version License PHP version Categories
sevent 1.0.0GNU General Publi...5HTTP, PHP 5, Web services, AJAX
Description Author

This package can send data of server events to the browsers.

It provides a PHP class and a jQuery plugin to respond to browser requests to listen to events happening on the server.

The PHP class calls a given callback function that can call the class functions with arbitrary names and marshals the function name as the event name and the respective parameters to the browser in JSON format.

On the browser side the jQuery plugin can register handler functions for each type of server side event that may happen.

Innovation Award
PHP Programming Innovation award nominee
November 2016
Number 2


Prize: One big elePHPant Plush Mascott
Server sent events are events that happen on the server side and are notified immediately to the browser accessing the current page.

This package makes it easy to dispatch server side events to the browser by calling functions with the name of the events you want to dispatch.

On the browser side it handles the events with JavaScript handler functions that are called when the a server side event notification is received.

Manuel Lemos
  Performance   Level  
Name: Mohamed Elbahja is available for providing paid consulting. Contact Mohamed Elbahja .
Classes: 9 packages by
Country: Morocco Morocco
Age: 22
All time rank: 11381 in Morocco Morocco
Week rank: 19 Up1 in Morocco Morocco Equal
Innovation award
Innovation award
Nominee: 3x

Details

Sevent

Sevent: Server-Sent Events PHP & JQuery Plugin

HTML & js

<!DOCTYPE html>
<html>
<head>
	<title>hello</title>
</head>
<body>
<script src="path/to/jquery.js"></script>
<script src="sevent.js"></script>
<script type="text/javascript">
		
	$(document).ready(function() {

		$.sevent.init({
			url: 'http://your-project/sevent.php',
		});

		$.sevent.on('open', function(event) {

			console.log('open'); 
		});

		$.sevent.on('message', function(response) {

			// response is a server response
			console.log('server response :' + response.data);
		});
		
	});	
</script>
</body>
</html>

PHP

<?php
// sevent.php

require_once('Sevent.class.php');

$event = new Sevent();

$sevent->header();

$event->response(function() use ($event) {
     
     // conditions and code here
		
    $event->message('hello world'); // sent a response message
});

Custom Events

Js

$(document).ready(function() {

		$.sevent.init({
			url: 'http://your-project/sevent.php',
		});

		$.sevent.on('message', function(response) {
    
			// response is a server response
			console.log(response.data);
		});
    
    	$.sevent.on('news', function(response) {
    
			// your code here for ex: 
      	$('#news').append(response.data);
			console.log(response);
		});
    
    	$.sevent.on('newComment', function(response) {
    
			// your code here for ex: 
      	$('#comments').append(response.data);
			console.log(response);
		});
    
    	$.sevent.on('anythingBlaBla', function(data) {
    
			// anything code data is a server response
		});
		
});	

PHP


<?php
// sevent.php

require_once('Sevent.class.php');

$event = new Sevent();

$sevent->header();


$event->response(function() use ($event) {
     
     // conditions and code here
     
     $db = new mysqli('host', 'user', 'pass', 'db');
     
     $event->news('news conents');
     
     $blabla = $db->query('SELECT * FROM BlaBla');
     
     if ($blabla->num_rows > 0) {
        
        $event->anythingBlaBla('content here');
     }
		
    //if ( message true ) {
    
     // $event->message('hello world'); // sent a response message
   //}
   
   // if (newComment true) {

      // $event->newComment(' new comments conetnt here');
       
    //}   
   
});

IE Not Supported :)


$(document).ready(function() {

	$.sevent.init({
		url: 'http://your-project/sevent.php',

		// if browser not supported EventSource
		notSupported: function () {
			// your code 
			alert('your browser not supported, please download xName browser');
		}
	});
		
});

How to Close Event


$.sevent.exit();

Sevent And JSON

php


<?php
// sevent.php

require_once('Sevent.class.php');

$event = new Sevent();

$sevent->header();

$event->response(function() use ($event) {
     
     // conditions and code here
     
     $yourCond = true;

     if ($yourCond === true) {

     	$response = new \stdClass;

     	$response->type = 'like';
     	$response->text = 'Mohamed like your image';
     	$response->url  = '/images/id/1111';

     	$event->notification( json_encode($response) );

     }
   
});

js

$(document).ready(function() {

		$.sevent.init({
			url: 'http://your-project/sevent.php',
			notSupported: function () {
				alert('your browser not supported');
			}
		});

    
    	$.sevent.on('notification', function(response) {
    		
    		data = $.sevent.json(response);

    		if (data !== false) {

    			if (data.type === 'like') {

    				$('#notificationId').append('<a herf="'+ data.url +'"> ' + data.text + ' </a>');
    			
    			} else if (data.type === 'comment') {

    				// code
    			}
    		
    		}

		});
		
});	

Custom Headers


require_once('Sevent.class.php');

$event = new Sevent();


$sevent->header(array(
	'Cache-Control' => 'no-cache',
	'Connection' 	=> 'Keep-alive', 
	// ...
));

Custom Response Options

require_once('Sevent.class.php');

$event = new Sevent();

$sevent->header();

//$event->eventName(string data, array options);

$event->notification('data', array(
	'id'    => 'ssss',
	'retry' => 5000, // 5sc
));

  Files folder image Files  
File Role Description
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file Sevent.class.php Class Class source
Accessible without login Plain text file sevent.js Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:278
This week:0
All time:7,317
This week:543Up
 User Ratings  
 
 All time
Utility:58%StarStarStar
Consistency:58%StarStarStar
Documentation:58%StarStarStar
Examples:-
Tests:-
Videos:-
Overall:40%StarStarStar
Rank:2889