https://github.com/melbahja/sevent
Sevent: Server-Sent Events PHP & JQuery Plugin
https://github.com/melbahja/sevent
Last synced: about 2 months ago
JSON representation
Sevent: Server-Sent Events PHP & JQuery Plugin
- Host: GitHub
- URL: https://github.com/melbahja/sevent
- Owner: melbahja
- License: gpl-3.0
- Created: 2016-11-26T17:46:55.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-26T18:56:15.000Z (almost 9 years ago)
- Last Synced: 2025-04-09T20:07:09.161Z (6 months ago)
- Language: PHP
- Size: 17.6 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sevent
Sevent: Server-Sent Events PHP & JQuery Plugin### HTML & js
```htmlhello
$(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);
});
});```
### PHP
```php
header();$event->response(function() use ($event) {
// conditions and code here
$event->message('hello world'); // sent a response message
});```
## Custom Events
### Js
```javascript
$(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
```phpheader();
$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 :)
```javascript
$(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
```javascript
$.sevent.exit();
```
### Sevent And JSON
#### php
```php
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
```javascript
$(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(' ' + data.text + ' ');
} else if (data.type === 'comment') {// code
}
}});
});
```#### Custom Headers
```php
require_once('Sevent.class.php');
$event = new Sevent();
$sevent->header(array(
'Cache-Control' => 'no-cache',
'Connection' => 'Keep-alive',
// ...
));```
#### Custom Response Options
```php
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
));```