Skip to content

PredaEngine/server-sent-event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

server-sent-event

JavaScript

var evtSource = new EventSource("ssedemo.php");
evtSource.onmessage = function(e) {
  var newElement = document.createElement("li");
  
  newElement.innerHTML = "message: " + e.data;
  eventList.appendChild(newElement);
}

Source : https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events More : http://dev.w3.org/html5/eventsource/

PHP

In ssedemo.php

<?php
header("Content-Type: text/event-stream");
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.

include 'ServerSentEvent.php';

$event = ServerSentEvent::getInstance();
$event->process(function($manager){
	$manager->send(array(
		'event' => 'ping',
		'data' => json_encode(array('time' =>time())),
		));
},20);

This code will send an event 'ping' to the client with data the current time (time()) every 20 seconds. The array passed to the send function can contain three indexes:

  • id (optional) : the name of the event
  • event : The name of the event
  • data (array or string) : The data which will be send to the client

About

ServerSentEvent.php is a PHP class that allows you to emit events that can receiving Server-Sent Events in HTML5.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages